网络编程
位置:首页>> 网络编程>> Python编程>> Python实现将Excel内容插入到Word模版中

Python实现将Excel内容插入到Word模版中

作者:白马百度  发布时间:2022-05-04 23:21:03 

标签:Python,Excel,Word

前言

前段时间因为需要处理一大堆验收单,都是一些简单的复制粘贴替换工作,于是就想到用python进行处理。接下来进入正题~

实现需求

我是用的开发环境是

  • python 3.6

  • openpyxl 3.1.1

  • docx 0.2.4

需求

Python实现将Excel内容插入到Word模版中

这个是从公司平台导出的订单详情excel文件

Python实现将Excel内容插入到Word模版中

这个是公司验收单模版

我这边需求是把Excel文件中的订单号、下单公司、套餐、数量分别添加到模版的订单编号、甲方、验收测试内容中,简单来说就是通过python脚本,将excel文件的订单号、下单公司、套餐、数量分别替换word文件中的OrderID、Company、Package、Quantity

实现代码

明确需求后直接上代码

import openpyxl
import docx
import datetime
def get_excel_data():
   # 打开Excel文件
   wb = openpyxl.load_workbook('下单明细.xlsx')
   ws = wb['Sheet1']
   # 获取序列号
   for cell in ws['A']:
       Number.append(cell.value)
   # 获取订单号
   for cell in ws['C']:
       OrderID.append(cell.value)
   # OrderID.pop(0)
   # 获取数量
   for cell in ws['F']:
       Quantity.append(cell.value)
   # 获取公司名称
   for cell in ws['B']:
       Company.append(cell.value)
   # 获取订单套餐
   for cell in ws['D']:
       Package.append(cell.value)
   # 替换word文档内容
   for i in range(len(Number)):
       # 打开word文档
       new_doc = docx.Document('交付验收单.docx')
       for p in new_doc.paragraphs:
           for r in p.runs:
               # print(r.text)
               if 'OrderID' in r.text: # 替换订单号
                   item = OrderID[i]
                   r.font.underline = True
                   r.text = r.text.replace('OrderID', item)
                   print('OrderID' + '更改为' + str(item))
               if 'Quantity' in r.text: # 替换数量
                   item = Quantity[i]
                   r.font.underline = True
                   r.text = r.text.replace('Quantity', str(item))
                   print('Quantity' + '更改为' + str(item))
               if 'Company' in r.text: # 替换公司名称
                   item = Company[i]
                   r.font.underline = True
                   r.text = r.text.replace('Company', str(item))
                   print('Company' + '更改为' + str(item))
               if 'Package' in r.text:  # 替换订单套餐
                   item = Package[i]
                   r.font.underline = True
                   r.text = r.text.replace('Package', str(item))
                   print('Package' + '更改为' + str(item))
                   # 替换日期    #这里因为可以直接改模版所有注释掉了,需要可开启
               # if 'Yy' in p.text:
               #     p.text = p.text.replace('Yy', str(year))
               # if 'Mm' in p.text:
               #     p.text = p.text.replace('Mm', str(month))
               # if 'Dd' in p.text:
               #     p.text = p.text.replace('Dd', str(day))
       # 保存新文档    #文件命名格式:交付验收单-公司名称时间序号.docx
       new_doc.save('交付验收单-'+ str(Company[i]) +str(year)+str(month)+str(day)+'-' + str(Number[i]) + '.docx')
if __name__ == "__main__":
   Number = []
   OrderID = []
   Quantity = []
   Company = []
   Package = []
   now = datetime.datetime.now()
   year = now.strftime("%Y")
   month = now.strftime("%m")
   day = now.strftime("%d")
   get_excel_data()

运行效果

终端:

Python实现将Excel内容插入到Word模版中

文件夹保存文件:

Python实现将Excel内容插入到Word模版中

注意:这里我为了方便以及更直观的看到效果,把Excel文件表头栏也进行替换了,后续如果需要可以使用

OrderID.pop(0)将表头栏参数删掉,再把for循环次数减一即可

for i in range(len(Number) - 1):替换后的word文件:

Python实现将Excel内容插入到Word模版中

来源:https://segmentfault.com/a/1190000043515136

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com