重新定位Excel图表时,我将它们添加到同一PowerPoint幻灯片中
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道Python3如何声明变量或以其他方式工作,但我会尝试如下所示。相反,
我会尝试
其中 shp 是代表粘贴形状的变量。
I don't know how Python3 declares variables or otherwise works, but I would try something like the following. Instead of
I would try
where shp is a variable that represents the pasted shape.