读取word到excel

#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")

   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值