使用python为Excel插入附件

客户有这么个需求,但查阅了百度谷歌,似乎python常用的那些操作excel的库都不满足

这个方法要求运行环境是windows,而且要预装Excel。

直接列代码了

import pythoncom
pythoncom.CoInitialize()
from win32com.client import Dispatch
import win32com.client

filename = "D:\Book1.xlsx"  
xlApp = Dispatch('Excel.Application')
xlApp.Visible = True #显式打开excel 调试设置True
book = xlApp.Workbooks.Add()  
xlSheet =  book.Worksheets(1)
xlSheet.Cells(1,1).Value = 'title'
xlSheet.Cells(2,1).Value = 123
shape = xlSheet.Shapes.AddOLEObject(ClassType='Paint.Picture',Filename="D:\union.jpeg", Link=False)  #插图片附件
shape.Left = xlSheet.Cells(2,2).Left  #把定位附件到指定单元格 单位:磅
shape.Top = xlSheet.Cells(2,2).Top
xlSheet.Rows(2).RowHeight  = shape.Height    #行高
xlSheet.Columns(2).ColumnWidth = shape.Width  #列宽
book.SaveAs(filename) 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值