读取 zip 文件中的 docx 文件而不解压缩

时间:2021-04-26 11:08:08

标签: python

我正在尝试从 zip 文件中的 docx 文件中读取信息。 zip 文件不在我的磁盘中,我使用 REST API 检索它,仅供您知道。

我尝试了很多方法,但都没有奏效。我设法使用 zipfile 库读取 zip 文件并查看其中的所有文档,包括 docx 文件,也设法以字节为单位打印 docx 文件,但是每当我尝试将其解码为 utf-8 时,我都会收到错误消息:

"utf-8' codec can't decode byte 0x8b in position 1: invalid start byte"

如果我尝试使用 latin-1 或任何其他解码器进行解码,它会返回我根本无法理解的字符串。

这是我尝试使用的代码:

file1 = io.BytesIO(response.content) 
file = zipfile.ZipFile(file1)

file_names = file.namelist()
word_file = file.namelist()[1]  #the position of the word file in the zip file

file_word = file.open(word_file).read() 

我也尝试过使用 docx 库来打开 docx 本身,但它不起作用(我想是因为我以字节为单位)。
我的主要努力是真正将字节转换为可读的东西。 我只需要从 word 文件中检索一些数据,其中有一些文本和我需要的表格,但我无法提取它,因为我有超过一千个 zip 文件,每一个他们有几个文件(虽然我只想要 docx 文件)。

如果您能帮助我,我将不胜感激。谢谢。

0 个答案:

没有答案