python
from docx import Document
from docx.oxml.ns import qn
def iter_block_items(parent):
"""
生成 `parent` 元素的每个块级元素,包括段落和表格。
"""
parent_elm = parent.element.body
for child in parent_elm.iterchildren():
if child.tag == qn('w:p'):
yield 'p', child
elif child.tag == qn('w:tbl'):
yield 'tbl', child
# 打开文档
doc = Document("example.docx")
paragraphs = doc.paragraphs
tables = doc.tables
paragraph_index = 0
table_index = 0
for block_type, block in iter_block_items(doc):
if block_type == 'p':
paragraph = paragraphs[paragraph_index]
print("Paragraph:", paragraph.text)
paragraph_index += 1
elif block_type == 'tbl':
table = tables[table_index]
table_index += 1
if table:
print("Table:")
for n_r, row in enumerate(table.rows):
print(f" Row {n_r}: ")
for n_c, cell in enumerate(row.cells):
print(f" Column {n_c}:", cell.text, end=' ')
print()