如题,代码其实在之前的线上培训已经讲过。现分享该代码,并在基础上再次处理
具体代码如下:
from docx import Document # 读Word的库
from openpyxl import Workbook # 写Excel的库
“““file_path : Word文本的路径
save_path : 保存Excel的路径
”””
def word_table_excel(file_path,save_path):
document = Document(file_path)
count = 0
tables = []
wb = Workbook()
ws = wb.active
total = len(document.tables) # total 得到Word找那个有几个表格
print("总共", total, "个表格等待处理...")
for index in range(0, total):
table = []
for row in document.tables[index].rows:
line = []
for grid in row.cells:
line.append(grid.text)
table.append(line)
ws.append(line)
count = count + 1
print("第", count, "个表格正在处理...剩余", total - count + 1, "个表格", "\n")
tables.append(table)
wb.save(save_path)
print(tables)
print