python win32com 操作excel插入字段 word excel ppt 压缩包等文档

python EXCEL文档处理

代码作用

实现python 在excel指定位置写入字段,在指定位置插入word/excel/图片等文件
插入字段和附件

import sys, os
import win32com.client

baseDirPath = sys.path[0]   
print(baseDirPath)

info_file_path = baseDirPath + '\\files\\info.xlsx'
template_file_path = baseDirPath + '\\files\\template.xlsx'   
ICON_EXCEL = baseDirPath + '\\files\\WORDICON.EXE'   #EXCEL图标所在地址
ICON_WORD = baseDirPath + '\\files\\XLICONS.EXE'     #WORD图标所在地址

output_path = baseDirPath + '\\files\\output'
if not os.path.exists(output_path):
  os.makedirs(output_path)
  
rarType = ['rar', 'zip', '7z']
picType = ['jpg', 'png', 'bmp', 'jpeg']
excelType = ['xls', 'xlsx']
docType = ['doc','docx']

if __name__ == '__main__':

  #使用WPS,关闭WPS程序
  try:
    wps = win32com.client.gencache.EnsureDispatch('kwps.application')
  except:
    wps = win32com.client.gencache.EnsureDispatch('wps.application')
    
  try:
    wps.Quit
  except:
    pass
  
  excelApp = win32com.client.gencache.EnsureDispatch('Excel.Application')
  rel_wb = excelApp.Workbooks.Open(info_file_path)   #打开info.xlsx
  rel_ws = rel_wb.Worksheets(1)                      #excel第‘1’个sheet
  total_rows = rel_ws.UsedRange.Rows.Count
  total_cols = rel_ws.UsedRange.Columns.Count
  
  
  #打开模板excel
  temp_wb = excelApp.Workbooks.Open(template_file_path)   #打开模板文件
  temp_ws = temp_wb.Worksheets(1)
  print('temp_wb Excel open')
  ######插入字段  将info.xlsx中的字段写入到template.xlsx的B列
  temp_ws.Range('B2').Value = rel_ws.Cells(1, 'C').Value
  temp_ws.Range('B3').Value = rel_ws.Cells(1, 'D').Value
  temp_ws.Range('B4').Value = rel_ws.Cells(1, 'E').Value
  temp_ws.Range('B5').Value = rel_ws.Cells(1, 'F').Value
  
  ######插入附件
  #info.xlsx中'A1'信息为附件所在文件夹的名称
  insertFilePath = baseDirPath + '\\files\\AddFiles\\' + str(rel_ws.Cells(1, 'A').Value)
  #遍历所有待插入附件
  for root, dirs, files in os.walk(insertFilePath):
    for file in files:
      file_path = os.path.join(root, file)
      fType = file.split('.')[-1].lower()    #文件类型
      if '~$' in file:              #doc/excel 打开时的缓存文档
        continue
      if fType.lower() == 'pdf':    #暂无法导入pdf文档
        continue
      
      if fType in picType:          #图片类型 放在C2
        col = 'C'
        row = 2
        shape = temp_ws.Pictures().Insert(Filename = file_path)
        shape.Top = temp_ws.Cells(row, col).Top + 10
        shape.Left = temp_ws.Cells(row, col).Left + 10
        shape.Width = 50
      elif fType in rarType:        #压缩包类型 放在C5
        col = 'C'
        row = 5
        shape = temp_ws.Shapes.AddOLEObject(Filename = file_path, Link = False)
        shape.Top = temp_ws.Cells(row, col).Top + 10
        shape.Left = temp_ws.Cells(row, col).Left + 10
      elif fType in excelType:      #excel类型 放在C4
        icon_file_path = ICON_EXCEL
        add_file_name = file.split('.')[0]
        col = 'C'
        row = 4
        shape = temp_ws.Shapes.AddOLEObject(Filename = file_path, Link = False,
                  DisplayAsIcon = True, IconFileName = icon_file_path, IconIndex = 0, 
                  IconLabel = add_file_name)
        shape.Top = temp_ws.Cells(row, col).Top + 10
        shape.Left = temp_ws.Cells(row, col).Left + 10
      elif fType in docType:        #word类型 放在C3
        icon_file_path = ICON_WORD
        add_file_name = file.split('.')[0]
        col = 'C'
        row = 3
        shape = temp_ws.Shapes.AddOLEObject(Filename = file_path, Link = False,
                  DisplayAsIcon = True, IconFileName = icon_file_path, IconIndex = 0, 
                  IconLabel = add_file_name)
        shape.Top = temp_ws.Cells(row, col).Top + 10
        shape.Left = temp_ws.Cells(row, col).Left + 10
      
  excel_out_path = output_path + '\\outputExcel.xlsx'      
  temp_wb.SaveAs(excel_out_path)
  temp_wb.Close()
  excelApp.Application.Quit()
  print('finish--------------')

案例附件:
https://download.csdn.net/download/mjc1321/89064542

  • 14
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值