#coding:utf-8 import docx from docx import Document #导入库 import xlrd import xlwt import os def docx_to_xls(in_file,out_file): path = in_file; #"./2023年10月办公用品价格明细表.docx" #文件路径 document = Document(path) #读入文件 tables = document.tables #获取文件中的表格集 #定义一个workbook(文件),编码为utf-8 workbook = xlwt.Workbook(encoding='utf-8') #增加一个表单,cell_overwrite_ok=True表示可以覆盖原单元格中数据,默认为False,覆盖时会抛出异常 #sheet = workbook.add_sheet("对应的子表格", cell_overwrite_ok=True) sheet1 = workbook.add_sheet("子表格", cell_overwrite_ok=True) g_row = 0 for x in range(0,len(tables)): table = tables[x]#获取文件中的第一个表格 for i in range(0,len(table.rows)):#从表格第一行开始循环读取表格数据 for k in range(0,len(table.columns)): #result = table.cell(i,k).text + "" +table.cell(i,1).text+table.cell(i,2).text + table.cell(i,3).text sheet1.write(g_row,k, table.cell(i,k).text) #print(result) g_row = g_row + 1 workbook.save(out_file) #"2023年10月办公用品价格明细表.xls") if __name__=='__main__': docx_to_xls("./2023年10月办公用品价格明细表.docx","./gczx_122.xlsx")
读取word到excel
最新推荐文章于 2025-02-28 11:35:59 发布