## 安装 模块
pip install python-docx
## 使用
from docx import Document
## 读取 Word 文件里的表格信息
try:
doc = Document('A:\\ABC.docx')
except Exception as e:
print("读取 Word 文件失败", e)
else:
print("读取 Word 文件成功")
## 读取文件中所有表格
doc.tables # 表格数 len(doc.tables)
## 第1个表格
doc.tables[0]
## 第1个表格的全部行
doc.tables[0].rows # 行数 len(doc.tables[0].rows)
## 第1个表格的 第1行
doc.tables[0].rows[0]
## 第1个表格 的 第1行 的全部 列
doc.tables[0].rows[0].cells # 列数 len(doc.tables[0].rows[0].cells)
## 第1个表格 的 第1行 的 第1列
doc.tables[0].rows[0].cells[0]
doc.tables[0].rows[0].cells[0].text # 第1个表格 的 第1行 的 第1列 的单元格内容
## 遍历全部单元格(下标方式)
表格数量 = len(doc.tables)
#print("表格数量", 表格数量)
for 表格编号 in range(0, 表格数量):
#doc.tables[表格编号] ## 遍历每一个表格
行数 = len(doc.tables[表格编号].rows)
#print("行数", 行数)
for 行编号 in
Python3 读取word中的表格,根据表格第一行标题行查找想要的表格
最新推荐文章于 2024-07-03 17:25:46 发布