python 解析word每页都是一条路的情况说明通过标题’XXX情况说明‘分割后去所有路和其情况说明
要解析Word文档并提取每一页的路和情况说明,可以使用Python的python-docx库。下面是一个简单的示例代码:
from docx import Document
# 打开Word文档
doc = Document('document.docx')
# 存储所有路和情况说明的列表
roads = []
# 将每一页的路和情况说明提取出来
current_road = None
current_description = ""
for paragraph in doc.paragraphs:
if paragraph.text.startswith('XXX情况说明'):
# 如果遇到标题'XXX情况说明',将之前的路和情况说明添加到列表中
if current_road is not None:
roads.append((current_road, current_description))
# 开始新的路和情况说明
current_road = paragraph.text
current_description = ""
else:
# 将当前段落的文本添加到情况说明中
current_description += paragraph.text + "\n"
# 添加最后一条路和情况说明
if current_road is not None:
roads.append((current_road, current_description))
# 打印所有的路和情况说明
for road, description in roads:
print("路:", road)
print("情况说明:", description)
print()
请将代码中的document.docx替换为你要解析的Word文档的路径。代码将逐页遍历文档,当遇到标题为XXX情况说明时,将之前的路和情况说明添加到列表中,并开始新的路和情况说明。最后,将所有的路和情况说明打印出来。
注意:在运行代码之前,需要确保已经安装了python-docx库。可以使用以下命令进行安装:
pip install python-docx
``
原文地址: https://www.cveoy.top/t/topic/iRCT 著作权归作者所有。请勿转载和采集!