使用Python修改PDF文件
我正在使用这段代码把十六进制转换成字符,但我该怎么做才能在PDF文件中替换掉每一个找到的十六进制呢?
re.sub("#(..)", lambda match: chr(int(match.group(1), 16)), s)
我试过这个,但当我尝试打印的时候什么也没有出现。
import re
fh = open("C:\\Users\\Suleiman JK\\Desktop\\test\\Hello.pdf","r+")
stuff = fh.read()
re.sub("#(..)", lambda match: chr(int(match.group(1), 16)), stuff)
fh.close()
fh = open("C:\\Users\\Suleiman JK\\Desktop\\test\\Hello.pdf")
print fh.read()
有没有人能帮我解决这个问题?
1 个回答
0
你没有往文件里写任何东西。
import re
with open("C:\\Users\\Suleiman JK\\Desktop\\test\\Hello.pdf","rb") as fh:
stuff = fh.read()
stuff = re.sub("#(..)", lambda match: chr(int(match.group(1), 16)), stuff)
with open("C:\\Users\\Suleiman JK\\Desktop\\test\\Hello.pdf","wb") as fh:
fh.write(stuff)