开发者

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

开发者 https://www.devze.com 2023-03-10 09:19 出处:网络 作者: 白马百度
目录前言实现需求需求实现代码运行效果前言 前段时间因为需要处理一大堆验收单,都是一些简单的复制粘贴替换工作,于是就想到用python进行处理。接下来进入正题~
目录
  • 前言
  • 实现需求
    • 需求
  • 实现代码
    • 运行效果

      前言

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

      实现需求

      我是用的开发环境是

      • python 3.6
      • openpyxl 3.1.1
      • docx 0.2.4

      需求

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

      这个是从公司平台导android出的订单详情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('Owww.devze.comrderID' + '更改为' + 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:
                      #     pjavascript.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]) + 开发者_C培训'.docx')
      if __name__ == "__main__":
          Nujavascriptmber = []
          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模版中

      到此这篇关于Python实现将Excel内容插入到Word模版中的文章就介绍到这了,更多相关Python Excel内容插入到Word内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

      0

      精彩评论

      暂无评论...
      验证码 换一张
      取 消

      关注公众号