简体   繁体   English

如何使用 python-docx 模块读取“.doc”文件

[英]How to read '.doc' file with python-docx module

I'm trying to read the.doc file with python-docx module, I'm doing我正在尝试使用 python-docx 模块读取 .doc 文件,我正在做

import docx

path = 'Sample-doc-file-100kb.doc'
doc = docx.Document(path) 
#extracting texts from doc

This works fine for.docx but gives ValueError: file 'Sample-doc-file-100kb.doc' is not a Word file, content type is 'application/vnd.openxmlformats-officedocument.themeManager+xml' error for.doc file.这适用于 .docx,但会给出ValueError: file 'Sample-doc-file-100kb.doc' is not a Word file, content type is 'application/vnd.openxmlformats-officedocument.themeManager+xml'错误 for.doc 文件。

I searched and found that this docx module doesn't work for older version of doc file.我搜索并发现这个 docx 模块不适用于旧版本的 doc 文件。 And I looked for converting the doc to docx but all the solution are windows dependent.我寻找将文档转换为 docx,但所有解决方案都依赖于 windows。

I'm running this code on aws-lambda so can't use those method.我在 aws-lambda 上运行此代码,因此无法使用这些方法。

Any way to either convert to doc to docx (platform independent) or to read.doc file?有什么方法可以将 doc 转换为 docx(平台无关)或 read.doc 文件?

convert to doc to docx (platform independent)转换为 doc 到 docx(平台无关)

If you are able to provide working LibreOffice or OpenOffice then you might try using unoconv to do doc to docx conversion as it如果您能够提供可用的 LibreOffice 或 OpenOffice,那么您可以尝试使用unoconv进行 doc 到 docx 的转换,因为它

is a command line tool to convert any document format that LibreOffice can import to any document format that LibreOffice can export.是一个命令行工具,用于将 LibreOffice 可以导入的任何文档格式转换为 LibreOffice 可以导出的任何文档格式。

in Ubuntu with this command:在 Ubuntu 中使用以下命令:

apt-get install antiword

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM