gpt4 book ai didi

python - 在 docx 中按顺序处理对象

转载 作者:行者123 更新时间:2023-12-05 02:19:37 55 4
gpt4 key购买 nike

我想按照在 word 文档中写入的顺序处理对象。我遇到的对象是段落、段落中的文本、段落中的运行、运行中的文本、表格和表格单元格中的段落。到目前为止,我有两个有用的程序。一个遍历文档的段落并获取该段落的文本;存储在由 [paragraph #] 索引的列表中。同一个程序能够从运行中收集文本;存储在由 [paragraph#][run#] 索引的二维列表中,但我没有发现运行比段落的整个文本更有用。我的第二个程序遍历整个文档并找到表格。当它有一个表格时,它会按行、单元格和单元格中的段落遍历表格。

现在这些似乎是我实现目标的重要组成部分。我想按顺序收集文本。抽象地说,就好像有人在键盘上按住向右箭头,命令闪烁的文本光标移动。当文本光标移到对象上时,它通过标记对象的 # 和对象类型的几个索引存储它们。

假设我有子函数 paragraph_read 和 table_read。假设文档具有以下对象顺序: .我想通过这些并按以下顺序执行我的子功能:paragraph_read、paragraph_read、table_read、paragraph_read

我想知道我的程序是否可以像向右滑动光标一样一个对象一个对象地移动文档对象。

非常感谢您的帮助。谢谢。

-克里斯

最佳答案

您需要将此函数添加到代码中方便的地方:

from docx.document import Document
from docx.oxml.table import CT_Tbl
from docx.oxml.text.paragraph import CT_P
from docx.table import _Cell, Table
from docx.text.paragraph import Paragraph


def iter_block_items(parent):
"""
Yield 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
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('my_document.docx')

for block_item in iter_block_items(document):
if isinstance(block_item, Paragraph):
do_paragraph_thing(paragraph=block_item)
elif isinstance(block_item, Table):
do_table_thing(table=block_item)
else:
# raise an exception or do nothing or whatever. This branch would
# only be reached on an unforeseen error.

关于python - 在 docx 中按顺序处理对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42093013/

55 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com