重新定位Excel图表时,我将它们添加到同一PowerPoint幻灯片中

发布于 2025-01-20 15:05:26 字数 1446 浏览 2 评论 0原文

我正在使用Python3和Win32COM软件包从Excel文件自动复制图表并将其粘贴到PowerPoint演示文稿中。除非调整尺寸,否则图表并不能全部适合一个幻灯片,因此我将其宽度缩小为0.85。但是,我不确定如何将它们粘贴到特定的位置,因此它们都互相粘贴在彼此之间。我想我已经在Visual Basic中找到了一个解决方案(此处: https://www.mrexcel.com/board/board/threads/vba-positioning-chart-chart-chart-in-powerpoint-copy-pasting-copy-pasting-from-excel.1107603/ 和此处: vba:复制 +从excel中选择 +粘贴的图表到PowerPoint ),但我不知道任何VBA,所以不确定如何将其转换为Python。感谢您的帮助!

import os
import win32com.client as client
from win32com.client import constants        

xl = client.gencache.EnsureDispatch('Excel.Application')
ppt = client.gencache.EnsureDispatch('PowerPoint.Application')

wb = xl.Workbooks.Open(os.path.abspath("desiredFile.xlsx"))
prs = ppt.Presentations.Open(os.path.abspath("sample00.pptx"))

sheet = wb.Sheets('TheSheetINeed')


Slide = prs.Slides.Add(prs.Slides.Count,constants.ppLayoutBlank)

for ch in sheet.ChartObjects():
 ch.Activate()
 ch.Copy()
    
 Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
#Here is where I need help
    
prs.Save()
prs.Close()   
wb.Close(False)

ppt.Quit()
xl.Quit()

I am using Python3 and the win32com package to automatically copy charts from an excel file and paste them into a powerpoint presentation. The charts do not all fit on one slide unless they are resized, so I scale their width by 0.85. However, I am not sure how to paste them at specific locations so they are all pasted atop one another. I think I have found a solution in visual basic (here: https://www.mrexcel.com/board/threads/vba-positioning-chart-in-powerpoint-after-copy-pasting-from-excel.1107603/ and here: VBA: Copy + Paste Selected Charts from Excel to Powerpoint) but I don't know any VBA so am not sure how to translate it to python. Thank you for your help!

import os
import win32com.client as client
from win32com.client import constants        

xl = client.gencache.EnsureDispatch('Excel.Application')
ppt = client.gencache.EnsureDispatch('PowerPoint.Application')

wb = xl.Workbooks.Open(os.path.abspath("desiredFile.xlsx"))
prs = ppt.Presentations.Open(os.path.abspath("sample00.pptx"))

sheet = wb.Sheets('TheSheetINeed')


Slide = prs.Slides.Add(prs.Slides.Count,constants.ppLayoutBlank)

for ch in sheet.ChartObjects():
 ch.Activate()
 ch.Copy()
    
 Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
#Here is where I need help
    
prs.Save()
prs.Close()   
wb.Close(False)

ppt.Quit()
xl.Quit()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2025-01-27 15:05:26

我不知道Python3如何声明变量或以其他方式工作,但我会尝试如下所示。相反,

Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)

我会尝试

shp = Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
shp.Left = LeftValue
shp.Top = TopValue

其中 shp 是代表粘贴形状的变量。

I don't know how Python3 declares variables or otherwise works, but I would try something like the following. Instead of

Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)

I would try

shp = Slide.Shapes.PasteSpecial(constants.ppPasteShape).ScaleWidth(.85,0)
shp.Left = LeftValue
shp.Top = TopValue

where shp is a variable that represents the pasted shape.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文