前言
在前几天的文章中我们讲解了如何从Word表格中提取指定数据并按照格式保存到Excel中,今天我们将再次以一位读者提出的真实需求来讲解如何使用Python从Excel中计算、整理数据并写入Word中,其实并不难,主要就是以下两步:
- openpyxl读取Excel获取内容
- docx读写Word文件
那我们开始吧!
需求确认
首先来看下我们需要处理的Excel部分数据,因涉及隐私已经将数据皮卡丘化

可以看到数据非常多,并且还存在重复数据。而我们要做的就是对每一列的数据按照一定的规则进行计算、整理并使用Python自动填入到Word中,大致的要求如下


上面仅是部分要求,真实需要填入word中的数据要更多!

除了对按照格式进行处理并存入Word中指定位置之外,还有一个需求:最终输出的word文件名还需要按照一定规则生成:

OK,需求分析完毕,接下来看Python如何解决!
Python实现
首先我们使用Python对该Excel进行解析
  
   - 
    
     
    
    
     
      from openpyxl 
      import load_workbook
     
    
- 
    
     
    
    
     
      import os
     
    
- 
    
     
    
    
     
      # 获取桌面的路径
     
    
- 
    
     
    
    
     
      def GetDesktopPath():
     
    
- 
    
     
    
    
         
      return os.path.join(os.path.expanduser(
      "~"), 
      'Desktop')
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      path = GetDesktopPath() + 
      '/资料/' 
      # 形成文件夹的路径便后续重复使用
     
    
- 
    
     
    
    
     
      workbook = load_workbook(filename=path + 
      '数据.xlsx')
     
    
- 
    
     
    
    
     
      sheet = workbook.active 
      # 获取当前页
     
    
- 
    
     
    
    
     
      # 可以用代码获取数据范围,如果要批处理循环迭代也方便
     
    
- 
    
     
    
    
     
      # 获取有数据范围
     
    
- 
    
     
    
    
     
      print(sheet.dimensions)
     
    
- 
    
     
    
    
     
      # A1:W10
     
    
利用openpyxl读取单元格有以下几种用法
  
   - 
    
     
    
    
     
      cells = sheet[
      'A1:A4']  
      # 返回A1-A4的4个单元格
     
    
- 
    
     
    
    
     
      cells = sheet[
      'A'] 
      # 获取A列
     
    
- 
    
     
    
    
     
      cells = sheet[
      'A:C'] 
      # 获取A-C列
     
    
- 
    
     
    
    
     
      cells = sheet[
      5] 
      # 获取第5行
     
    
- 
    
     
    
    
     
      # 注意如果是上述用cells获取返回的是嵌套元祖
     
    
- 
    
     
    
    
     
      for cell 
      in cells:
     
    
- 
    
     
    
    
     
          print(cell[
      0].value) 
      # 遍历cells依然需要取出元祖中元素才可以获取值
     
    
- 
    
     
    
    
     
      # 获取一个范围的所有cell
     
    
- 
    
     
    
    
     
      # 也可以用iter_col返回列
     
    
- 
    
     
    
    
     
      for row 
      in sheet.iter_rows(min_row=
      1, max_row=
      3,min_col=
      2, max_col=
      4):
     
    
- 
    
     
    
    
         
      for cell 
      in row:
     
    
- 
    
     
    
    
     
              print(cell.value)
     
    
明白了原理我们就可以解析获取Excel中的数据了
  
   - 
    
     
    
    
     
      # SQE
     
    
- 
    
     
    
    
     
      SQE = sheet[
      'Q2'].
      value
     
    
- 
    
     
    
    
     
      # 供应商&制造商
     
    
- 
    
     
    
    
     
      supplier = sheet[
      'G2'].
      value
     
    
- 
    
     
    
    
     
      # 采购单号
     
    
- 
    
     
    
    
     
      C2_10 = sheet[
      'C2:C10'] 
      # 返回cell.tuple对象
     
    
- 
    
     
    
    
     
      # 利用列表推导式后面同理
     
    
- 
    
     
    
    
     
      vC2_10 = [str(cell[
      0].
      value) 
      for cell 
      in C2_10]
     
    
- 
    
     
    
    
     
      # 用set简易去重后用,连接,填word表用
     
    
- 
    
     
    
    
     
      order_num = 
      ','.
      join(
      set(vC2_10))
     
    
- 
    
     
    
    
     
      # 用set简易去重后用&连接,word文件名命名使用
     
    
- 
    
     
    
    
     
      order_num_title = 
      '&'.
      join(
      set(vC2_10))
     
    
- 
    
     
    
    
     
      # 产品型号
     
    
- 
    
     
    
    
     
      T2_10 = sheet[
      'T2:T10']
     
    
- 
    
     
    
    
     
      vT2_10 = [str(cell[
      0].
      value) 
      for cell 
      in T2_10]
     
    
- 
    
     
    
    
     
      ptype = 
      ','.
      join(
      set(vT2_10))
     
    
- 
    
     
    
    
     
      # 产品描述
     
    
- 
    
     
    
    
     
      P2_10 = sheet[
      'P2:P10']
     
    
- 
    
     
    
    
     
      vP2_10 = [str(cell[
      0].
      value) 
      for cell 
      in P2_10]
     
    
- 
    
     
    
    
     
      info = 
      ','.
      join(
      set(vP2_10))
     
    
- 
    
     
    
    
     
      info_title = 
      '&'.
      join(
      set(vP2_10))
     
    
- 
    
     
    
    
     
      # 日期
     
    
- 
    
     
    
    
     
      # 用datetime库获取今日时间以及相应格式化
     
    
- 
    
     
    
    
     
      import datetime
     
    
- 
    
     
    
    
     
      today = datetime.datetime.today()
     
    
- 
    
     
    
    
     
      time = today.strftime(
      '%Y年%m月%d日')
     
    
- 
    
     
    
    
     
      # 验货数量
     
    
- 
    
     
    
    
     
      V2_10 = sheet[
      'V2:V10']
     
    
- 
    
     
    
    
     
      vV2_10 = [
      int(cell[
      0].
      value) 
      for cell 
      in V2_10]
     
    
- 
    
     
    
    
     
      total_num = sum(vV2_10) 
      # 计算总数量
     
    
- 
    
     
    
    
     
      # 验货箱数
     
    
- 
    
     
    
    
     
      W2_10 = sheet[
      'W2:W10']
     
    
- 
    
     
    
    
     
      vW2_10 = [
      int(cell[
      0].
      value) 
      for cell 
      in W2_10]
     
    
- 
    
     
    
    
     
      box_num = sum(vW2_10)
     
    
- 
    
     
    
    
     
      # 生成最终需要的word文件名
     
    
- 
    
     
    
    
     
      title = f
      '{order_num_title}-{supplier}-{total_num}-{info_title}-{time}-验货报告'
     
    
- 
    
     
    
    
     
      print(title)
     
    
通过上面的代码,我们就成功的从Excel中提取出来数据,这样Excel部分就结束了,接下来进行word的填表啦,由于这里我们默认读取的word是.docx格式的,实际上读者的需求是.doc格式文件,所以windows用户可以用如下代码批量转化doc,前提是安装好win32com
  
   - 
    
     
    
    
     
      # pip install pypiwin32
     
    
- 
    
     
    
    
     
      from win32com 
      import client
     
    
- 
    
     
    
    
     
      docx_path = path + 
      '模板.docx'
     
    
- 
    
     
    
    
     
      # doc转docx的函数
     
    
- 
    
     
    
    
     
      def doc2docx(doc_path,docx_path):
     
    
- 
    
     
    
    
     
          word = client.Dispatch(
      "Word.Application")
     
    
- 
    
     
    
    
     
          doc = word.Documents.Open(doc_path)
     
    
- 
    
     
    
    
     
          doc.SaveAs(docx_path, 
      16)
     
    
- 
    
     
    
    
     
          doc.Close()
     
    
- 
    
     
    
    
     
          word.Quit()
     
    
- 
    
     
    
    
     
          print(
      '\n doc文件已转换为docx \n')
     
    
- 
    
     
    
    
     
      if 
      not os.path.exists(docx_path):
     
    
- 
    
     
    
    
     
          doc2docx(docx_path[:
      -1], docx_path)
     
    
不过在Mac下暂时没有好的解决策略,如果有思路欢迎交流,好了有docx格式文件后我们继续操作Word部分
  
   - 
    
     
    
    
     
      docx_path = path + 
      '模板.docx'
     
    
- 
    
     
    
    
     
      from docx 
      import Document
     
    
- 
    
     
    
    
     
      # 实例化
     
    
- 
    
     
    
    
     
      document = Document(docx_path)
     
    
- 
    
     
    
    
     
      # 读取word中的所有表格
     
    
- 
    
     
    
    
     
      tables = document.tables
     
    
- 
    
     
    
    
     
      # print(len(tables))
     
    
- 
    
     
    
    
     
      # 15
     
    
确定好每个表格数后即可进行相应的填报操作,table的用法和openpyxl中非常类似,注意索引和原生python一样都是从0开始
  
   - 
    
     
    
    
     
      tables[
      0].cell(
      1, 
      1).
      text = SQE
     
    
- 
    
     
    
    
     
      tables[
      1].cell(
      1, 
      1).
      text = supplier
     
    
- 
    
     
    
    
     
      tables[
      1].cell(
      2, 
      1).
      text = supplier
     
    
- 
    
     
    
    
     
      tables[
      1].cell(
      3, 
      1).
      text = ptype
     
    
- 
    
     
    
    
     
      tables[
      1].cell(
      4, 
      1).
      text = info
     
    
- 
    
     
    
    
     
      tables[
      1].cell(
      5, 
      1).
      text = order_num
     
    
- 
    
     
    
    
     
      tables[
      1].cell(
      7, 
      1).
      text = time
     
    
上面代码完成Word中这一部分表格

我们继续用Python填写下一个表格
  
   - 
    
     
    
    
     
      for i 
      in range(
      2, 
      11):
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      0).
      text = str(sheet[f
      'T{i}'].value)
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      1).
      text = str(sheet[f
      'P{i}'].value)
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      2).
      text = str(sheet[f
      'C{i}'].value)
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      4).
      text = str(sheet[f
      'V{i}'].value)
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      5).
      text = str(sheet[f
      'V{i}'].value)
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      6).
      text = 
      '0'
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      7).
      text = str(sheet[f
      'W{i}'].value)
     
    
- 
    
     
    
    
     
          tables[
      6].cell(i, 
      8).
      text = 
      '0'
     
    
- 
    
     
    
    
      
     
    
- 
    
     
    
    
     
      tables[
      6].cell(
      12, 
      4).
      text = str(total_num)
     
    
- 
    
     
    
    
     
      tables[
      6].cell(
      12, 
      5).
      text = str(total_num)
     
    
- 
    
     
    
    
     
      tables[
      6].cell(
      12, 
      7).
      text = str(box_num)
     
    

这里需要注意两个细节:
- word写入的数据需是字符串,所以从Excel获取的数据需要用str格式化
- 表格可能存在合并等其他情况,因此你看到的行数和列数可能不是真实的,需要用代码不断测试。
按照上面的办法,将之前从Excel中取出来的数据一一填充到Word中对应位置就大功告成!最后保存一下即可。
  
   - 
    
     
    
    
     
      document.save(path + 
      f'{title}.docx')
     
    
- 
    
     
    
    
     
      print(
      '\n文件已生成')
     
    
结束语
回顾上面的过程,其实从需求和文件格式上看,这次文件的读写解析任务较复杂,码代码和思考时间会较久,所以当我们在考虑使用Python进行办公自动化之前需要想清楚这个问题:这次需要完成的任务是否工作量很多,或者以后长期需要进行,用Python是否可以解放双手?如果不是,实际上手动就可以完成,那么就失去了自动化办公的意义!
注:本文使用的数据与源码可在公众号:早起Python内获取
转载:https://blog.csdn.net/weixin_41846769/article/details/105729205
