Problem
Referring to the code below, whenever I run this piece of code, it will find the charts all the sheets and exporting as an image. However, when it comes to chartObject.Chart.Export("chart" + str(i) + ".png")
, it always exports to document folder in window.
Question
Is there a way to declare a path for it to be exported? (P.s) I have tried reading the documentation but there is no information regarding to my question. Thanks.
Code
1 thing to note: this piece of code was answered in 1 of the stackoverflow question way back in 2018. Hence, I do not own the credit for it. But I am working on something similar. Thanks.
from win32com.client import Dispatch
app = Dispatch("Excel.Application")
workbook_file_name = 'Programmes.xlsx'
workbook = app.Workbooks.Open(Filename=workbook_file_name)
# WARNING: The following line will cause the script to discard any unsaved changes in your workbook
app.DisplayAlerts = False
i = 1
for sheet in workbook.Worksheets:
for chartObject in sheet.ChartObjects():
# print(sheet.Name + ':' + chartObject.Name)
chartObject.Chart.Export("chart" + str(i) + ".png")
i += 1
workbook.Close(SaveChanges=False, Filename=workbook_file_name)