简体   繁体   English

如何使用 python docx2txt 模块从 docx 文件中检索特定部分

[英]How to Retrieve a particular section from docx file using python docx2txt module

我正在使用 python docx2txt模块来处理 docx 文件。现在我可以通过使用这个模块来获取整个文档。但我的要求是逐节检索文档(例如单独使用其内容的标题)。请帮助我获取使用 docx2txt 库的特定部分或标题。

The docx2text module has no built-in function for that. docx2text模块没有内置函数。

That means you have a couple of options.这意味着你有几个选择。

  • Try to recognize section headers and sections from the converted text.尝试从转换后的文本中识别节标题和节。 This will probably be difficult because it is hard to distinguish a one-sentence paragraph from a section header.这可能会很困难,因为很难将一个句子的段落与节标题区分开来。
  • Open the docx file using the zipfile module.使用zipfile模块打开docx文件。 Then read the word/document.xml file from the zipfile and extract the information.然后从 zipfile 中读取word/document.xml文件并提取信息。 This will give you the complete XML structure, so it should be possible to recognize section headers.这将为您提供完整的 XML 结构,因此应该可以识别节标题。
  • Use python-docx , like in this question .使用python-docx ,就像在这个问题中一样

All computerized processing requires that the documents have a consistent internal structure.所有计算机化处理都要求文件具有一致的内部结构。 If you have a document that uses real section headers but also formatted lines to start a section, your conversion is bound to fail.如果您的文档使用真实的部分标题但也使用格式化的行来开始一个部分,那么您的转换肯定会失败。

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

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