如何使用python从docx文件中提取格式化数据

2024-04-23 09:53:46 发布

您现在位置:Python中文网/ 问答频道 /正文

示例: 我有非常相似的内容如下docx文件

Introduction

A. This is text

  1. This is second text

    1.1 more complex st

  2. Yes it is

I. Now Roman

我想将输出存储在json数据结构中。上面应该是

输出

{'A': 'This is text', '1': 'This is second text', '1.1': 'more complex st', '2': 'Yes it is', 'I': 'Now Roman'}

我现在的代码是

from docx import Document

document = Document('myDoc.docx')

for para in document.paragraphs:
    print para.text

但是这个代码的问题是段落文本不包含段落编号。它只包含段落内容。 例子 对于“A.这是文本”,段落文本只包含“这是文本”,但我想要“A。这是文本”。在

谢谢


Tags: text文本内容ismoreitthisnow
2条回答

首先,使用插件(https://github.com/thepankajsingh/extract-doc-add-ins)将Doc/Word转换为HTML。现在您可以轻松地解析HTML来获得键值对。在

使用python docx模块

像这样读取数据:

from docx import Document


document = Document('test.docx')

for para in document.paragraphs:
    print para.text

一旦你有了数据,你就可以建立你的字典了

相关问题 更多 >