Python实现实现Word表格转成表格转成Excel表格的示例代码表格的示例代码
主要介绍了Python实现Word表格转成Excel表格的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来
一起学习学习吧
准备工作准备工作
pip install docx
pip install openpyxl
具体代码具体代码
# 没有的先pip install 包名称
from docx import Document
from openpyxl import Workbook
document = Document('Docx文件路径.dicx')
count = 0
tables = []
wb = Workbook()
ws = wb.active
# 设置列数,可以指定列名称,有几列就设置几个,
# A对应列1,B对应列2,以此类推
# 只能处理列数一致的表格,不一致的请在word文档(转下行)
# 中处理好后,再运行程序
ws['A1'] = '国家'
ws['B1'] = '专利号'
ws['C1'] = '自己设置'
ws['D1'] = '懂?'
ws['E1'] = '5'
ws['F1'] = '6'
ws['G1'] = '7'
ws['H1'] = '8'
ws['I1'] = '9'
ws['J1'] = '10'
ws['K1'] = '11'
ws['L1'] = '12'
total = len(document.tables)
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, "个表格", "")
tables.append(table)
# 测试专用,测试前30条数据请打开注释
# if count == 30:
# break
wb.save("要保存xlsx的路径.xlsx")
print(tables)
print("表格处理完成...")
图片效果
评论0