Python采集docx文档中的表格,可以通过pip安装python-docx库实现。
由于docx文档中的表格可能不规范,如果需要采集指定格式的表格,可以有先将表格转换为列表,进行重组。
import docx
import json
import openpyxl
doc = docx.Document(r"data/报告.docx")
list2 = []
for i, table in enumerate(doc.tables):
list1= []
for row in table.rows:
list3 = []
for cell in row.cells:
if cell.text.strip()!='': # 不采集表格内容为空的表格,如果需要,可以进行限制
list3.append(cell.text)
list1.append(list3)
if list1[0][0] =='序号':
for n in range(1,len(list1)):
list2.append(list1[n])