Python:使用win32com在特定行和列中将对象插入excel [英] Python: Insert object into excel in a specific row and column with win32com

查看:66
本文介绍了Python:使用win32com在特定行和列中将对象插入excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.5和win32com通过以下方法将对象(.pdf文件)插入excel:

I'm using Python 3.5 and win32com to insert an object (.pdf file) into excel with the following method:

Embedded_object.Add(ClassType=None, Filename=file_loction, Link=False, DisplayAsIcon=True, Left=3, Top=0, Width=50, Height=50)

这很好,但是它总是将对象嵌入到A1单元格中,是否可以使用上述方法将对象嵌入到特定的行和列中?

This works fine, however it always embeds the object in the A1 cell, is there a way to embed the object into a specific row and column using the above method?

我还尝试了以下方法:

worksheet.Range('A1:A1').Copy()
worksheet.Paste(Destination=worksheet.Range('C2:C2'))

它将对象放置在特定的单元格中,但也将对象放在A1:A1中的对象后面,因此它还不是真正的解决方案

It puts the object in a specific cell, but also what's behind the object in A1:A1, so it's not really a solution yet

推荐答案

您可以指定目标单元格,然后将这些值传递给以下项的 Left Top 属性 OLEObject 像这样:

You can specify a destination cell, and then pass those values to the Left and Top properties of the OLEObject like this:

import win32com.client as win32

xl = win32.gencache.EnsureDispatch('Excel.Application')
wb = xl.Workbooks.Open('file_name')
ws = wb.Worksheets("sheet_name")
dest_cell = ws.Range("C2") #change to your wanted location
obj = ws.OLEObjects()
obj.Add(ClassType=None, Filename='file_path', Link=False, DisplayAsIcon=True, Left=dest_cell.Left, Top=dest_cell.Top, Width=50, Height=50)
wb.Save()
xl.Application.Quit()

这篇关于Python:使用win32com在特定行和列中将对象插入excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆