我正在使用python olefile库修改.xls和.doc文件。有趣的是,下面的代码适用于.xls文件,但不适用于.doc文件。你知道吗
不知何故,.doc确实对其内容进行了不同的编码/处理。在.xls中,当打印raw_content
时,我看到许多奇怪的字节,但也看到完整的单词。你知道吗
在.doc文件中,我看到更多的\x00
,完整的单词就像在每个字符之间有一个\x00
。你知道吗
同样奇怪的是,olefile docs建议这样做,甚至在链接示例中谈论单词。你知道吗
file = open('my.doc', 'rb')
ole = olefile.OleFileIO(file.read())
raw_content = ole.openstream('WordDocument').read()
raw_content= raw_content.replace(
b'a-placeholder',
bytes(dynamic_value, 'utf-8') # made sure they are the same length!
)
# this works!
# raw_content= raw_content.replace(
# b'a',
# b'Z'
# )
ole.write_stream('WordDocument', raw_content)
ole.fp.seek(0)
file_contents = ole.fp.read()
# further processing...
目前没有回答
相关问题 更多 >
编程相关推荐