Python处理word文档

Python处理word文件

高版本word可以转低版本word ,反之不行。

  • doc(通过win32接口,转换成docx进行操作)
  • docx(Python-docx)
读docx(读word里的表格,):
	 data = docx.Document(os.path.join(path, "{}".format(name)))
            table = data.tables[0].rows[0].cells # 所有行索引
            ta = data.tables
            s = [i.text for i in table]
            # 培育对象姓名   企业名称
            # 根据行索引确定位置
            for index, i in enumerate(s):
                print(index, i)
                if '城区' in i:
                    chengqu = index
                if '对象' in i or '申报人' in i:
                    shenbaoduixiang = index
                if '名称' in i:
                    mingxheng = index
              # 再取数据
 			for i in ta:
                rows = i.rows
                print(len(rows))
                for j in range(1, len(rows)):
                    dic = {}
                    name = ta[0].cell(j, shenbaoduixiang).text
                    company = ta[0].cell(j, mingxheng).text
                    area = ta[0].cell(j, chengqu).text
                    dic['name'] = name
                    dic['company'] = company
                    dic['area'] = area
                    dic['city'] = '杭州'
                    sum.append(dic)

win32(doc–>docx)

				abspath = os.path.abspath('.').split('.')[0]
                full = os.path.join(abspath, 'outstanding_talent')
                # win32接口:读文件 支持绝对路径;
                word = wc.Dispatch("Word.Application")
                doc = word.Documents.Open(os.path.join(full, "{}".format(name)))
                new_name = name.split('.')[0]+'.docx'
                doc.SaveAs(os.path.join(full, "{}".format(new_name)), 12)
                doc.Close()
                word.Quit()
                os.remove(os.path.join(full, "{}".format(name)))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猿来是小贝壳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值