Dopo aver passato ore a provare cose diverse, ho trovato la soluzione a questo problema. Speriamo che aiuti qualcuno a risparmiare tempo. Il seguente codice copierà tutti i grafici da "workbook_with_charts.xlsx" a "Final_PowerPoint.pptx".
Per qualche ragione, che devo ancora capire, funziona meglio quando si esegue questo programma Python dal terminale CMD. A volte si rompe se si tenta di eseguirlo più volte, anche se la prima esecuzione di solito è OK.
Un altro problema è che nella quinta riga, se si attiva False utilizzando "presentation = PowerPoint.Presentations.Add (False)", non funziona con Microsoft Office 2013, anche se sia "True" che "False" funzionerà ancora con Microsoft Office 2010.
Sarebbe bello se qualcuno potesse chiarire questi due problemi.
# importing the necessary libraries
import win32com.client
from win32com.client import constants
PowerPoint=win32com.client.Dispatch("PowerPoint.Application")
Excel=win32com.client.Dispatch("Excel.Application")
presentation=PowerPoint.Presentations.Add(True)
workbook=Excel.Workbooks.Open(Filename="C:\\.........\\workbook_with_charts.xlsx",ReadOnly=1,UpdateLinks=False)
for ws in workbook.Worksheets:
for chart in ws.ChartObjects():
# Copying all the charts from excel
chart.Activate()
chart.Copy()
Slide=presentation.Slides.Add(presentation.Slides.Count+1,constants.ppLayoutBlank)
Slide.Shapes.PasteSpecial(constants.ppPasteShape)
# WE are going to make the title of slide the same chart title
# This is optional
textbox=Slide.Shapes.AddTextbox(1,100,100,200,300)
textbox.TextFrame.TextRange.Text=str(chart.Chart.ChartTitle.Text)
presentation.SaveAs("C:\\...........\\Final_PowerPoint.pptx")
presentation.Close()
workbook.Close()
print 'Charts Finished Copying to Powerpoint Presentation'
Excel.Quit()
PowerPoint.Quit()
Si può provare OprnPyXl di leggere il grafico dal file di Excel, che credo che esso sostiene, scrivere in un buffer di file e quindi aggiungere a PowerPoint. EDIT: 0penPyXl non supporta ancora la lettura di grafici quindi dubito che questo sia possibile in Python. –