使用 python-docx 检索具有文档结构的文档内容
Retrieve document content with document structure with python-docx
我必须从 docx
文件中检索表格和 previous/next 段落,但无法想象如何使用 python-docx
获取这些内容
我可以通过document.paragraphs
得到一个段落列表
我可以通过 document.tables
获取表格列表
如何获得像这样的文档元素的有序列表
[
Paragraph1,
Paragraph2,
Table1,
Paragraph3,
Table3,
Paragraph4,
...
]?
python-docx
还没有 API 支持;有趣的是,Microsoft Word API 也没有。
但您可以使用以下代码解决此问题。请注意,它有点脆弱,因为它使用了可能会发生变化的 python-docx
内部结构,但我希望它在可预见的未来会工作得很好:
#!/usr/bin/env python
# encoding: utf-8
"""
Testing iter_block_items()
"""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from docx import Document
from docx.document import Document as _Document
from docx.oxml.text.paragraph import CT_P
from docx.oxml.table import CT_Tbl
from docx.table import _Cell, Table
from docx.text.paragraph import Paragraph
def iter_block_items(parent):
"""
Generate a reference to each paragraph and table child within *parent*,
in document order. Each returned value is an instance of either Table or
Paragraph. *parent* would most commonly be a reference to a main
Document object, but also works for a _Cell object, which itself can
contain paragraphs and tables.
"""
if isinstance(parent, _Document):
parent_elm = parent.element.body
# print(parent_elm.xml)
elif isinstance(parent, _Cell):
parent_elm = parent._tc
else:
raise ValueError("something's not right")
for child in parent_elm.iterchildren():
if isinstance(child, CT_P):
yield Paragraph(child, parent)
elif isinstance(child, CT_Tbl):
yield Table(child, parent)
document = Document('test.docx')
for block in iter_block_items(document):
print('found one')
print(block.text if isinstance(block, Paragraph) else '<table>')
这里有更多的讨论:
https://github.com/python-openxml/python-docx/issues/276
解析为属性Document.story,包含文档顺序的段落和表格
https://github.com/python-openxml/python-docx/pull/395
document = Document('test.docx')
document.story
我必须从 docx
文件中检索表格和 previous/next 段落,但无法想象如何使用 python-docx
我可以通过document.paragraphs
我可以通过 document.tables
如何获得像这样的文档元素的有序列表
[
Paragraph1,
Paragraph2,
Table1,
Paragraph3,
Table3,
Paragraph4,
...
]?
python-docx
还没有 API 支持;有趣的是,Microsoft Word API 也没有。
但您可以使用以下代码解决此问题。请注意,它有点脆弱,因为它使用了可能会发生变化的 python-docx
内部结构,但我希望它在可预见的未来会工作得很好:
#!/usr/bin/env python
# encoding: utf-8
"""
Testing iter_block_items()
"""
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from docx import Document
from docx.document import Document as _Document
from docx.oxml.text.paragraph import CT_P
from docx.oxml.table import CT_Tbl
from docx.table import _Cell, Table
from docx.text.paragraph import Paragraph
def iter_block_items(parent):
"""
Generate a reference to each paragraph and table child within *parent*,
in document order. Each returned value is an instance of either Table or
Paragraph. *parent* would most commonly be a reference to a main
Document object, but also works for a _Cell object, which itself can
contain paragraphs and tables.
"""
if isinstance(parent, _Document):
parent_elm = parent.element.body
# print(parent_elm.xml)
elif isinstance(parent, _Cell):
parent_elm = parent._tc
else:
raise ValueError("something's not right")
for child in parent_elm.iterchildren():
if isinstance(child, CT_P):
yield Paragraph(child, parent)
elif isinstance(child, CT_Tbl):
yield Table(child, parent)
document = Document('test.docx')
for block in iter_block_items(document):
print('found one')
print(block.text if isinstance(block, Paragraph) else '<table>')
这里有更多的讨论:
https://github.com/python-openxml/python-docx/issues/276
解析为属性Document.story,包含文档顺序的段落和表格
https://github.com/python-openxml/python-docx/pull/395
document = Document('test.docx')
document.story