python - python-docx:查找表位于 ms word 文档中的标题名称
问题描述
我正在努力寻找表格所在的标题名称,我正在使用 python-docx 库,我想知道我可以使用它来获取表格所在的标题名称。
from docx import Document
from docx.shared import Inches
document = Document('test.docx')
tabs = document.tables
解决方案
您可以使用 xml 从 docx 文件中提取结构化信息。尝试这个:
doc = Document("file.docx")
headings = [] #extract only headings from your code
tables = [] #extract tables from your code
tags = []
all_text = []
schema = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'
for elem in doc.element.getiterator():
if elem.tag == schema + 'body':
for i, child in enumerate(elem.getchildren()):
if child.tag != schema + 'tbl':
node_text = child.text
if node_text:
if node_text in headings:
tags.append('heading')
else:
tags.append('text')
all_text.append(node_text)
else:
tags.append('table')
break
在上面的代码之后,您将获得显示文档标题、文本和表格结构的标签列表,然后您可以从列表中映射相应的数据。
还要检查标签列表中的数据以获取表格的标题。
推荐阅读
- javascript - 更新 youtube 隐私状态正在制作ForKidsModificationNotAllowed 错误
- java - 我无法在 android studio 中获取 firebase 数据值的总和
- kotlin - 如何制作
我试图使编辑文本不可移动,但我不知道如何,请你帮帮我。我只使用相对布局
<EditText android:id="@+id/textinput" android:layout_width="331dp" androi
- laravel-7 - 我有一张考勤表,其中包含 staff_id、absent_year、absent_month、absent_day 作为 laravel 中的主键,更新时我面临以下错误
- swift - macCatalyst 应用程序中的自定义方案 URL 不起作用
- reactjs - 在 forwardRef 回调中使用时,调用 React 钩子 useRef 和 useEffect 给出无效的钩子错误
- maven - 动态加载`buildscript`的本地`maven`存储库路径
- typescript - 在打字稿中将泛型类型转换为枚举
- vue.js - 不同状态下如何改变v-badge的颜色?
- android - Material Design UI Android中的TextField