Get Excel Chart Details To Python
I have an excel file which has the charts on a separate sheets and they have multiple series plotted on them. What I need to do is to plot exactly the same ranges using python. By
Solution 1:
Try this code:
Option 1 for all sheets
import win32com.client as client
defseries_output(chart):
print(f'\tChart name is {chart.Name}')
for sc in chart.SeriesCollection():
print(f'\t\t{sc.Name}: {sc.Formula}')
xl = client.Dispatch('Excel.Application')
wb = xl.Workbooks.Open(r'c:\test\Charts.xlsm')
for sh in wb.Sheets:
print(f'Processed sheet {sh.Name}')
if sh.Type == -4100: # it's chart sheet
series_output(sh)
elif sh.Type == -4167: # it's worksheet with (possible) chartsfor ch in sh.ChartObjects():
series_output(ch.Chart)
wb.Close(False) # don't save & close workbook
xl.Quit()
Option 2 for a specific sheet
import win32com.client as client
defseries_output(chart):
print(f'\tChart name is {chart.Name}')
for sc in chart.SeriesCollection():
print(f'\t\t{sc.Name}: {sc.Formula}')
xl = client.Dispatch('Excel.Application')
wb = xl.Workbooks.Open(r'c:\test\Charts.xlsm') # your own path\name
sh = wb.Sheets('Sheet1') # your own worksheet nameprint(f'Processed sheet {sh.Name}')
for ch in sh.ChartObjects():
series_output(ch.Chart)
wb.Close(False) # don't save & close workbook
xl.Quit()
Post a Comment for "Get Excel Chart Details To Python"