irate_oxidase
irate_oxidase

Reputation: 11

Repositioning Excel Charts as I add them into the same powerpoint slide

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()

Upvotes: 1

Views: 653

Answers (1)

Jon Peltier
Jon Peltier

Reputation: 6073

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.

Upvotes: 0

Related Questions