Search code examples
pythonexcelchartsopenpyxl

how to get excel chart's main title text using python openpyxl library


Using the Python module openpyxl, I'm attempting to obtain the excel chart's main title.

I need your assistance writing the script.

For example, the chart title "Test" is below. I want to retrieve this title and put it in a Python variable.

enter image description here


Solution

  • Specifically, you want something like this;

    import openpyxl
    
    # Load the Excel workbook
    workbook = openpyxl.load_workbook(r"chart.xlsx")
    
    # Access the sheet containing the chart
    sheet = workbook['charts - APAC']  # Replace with your sheet name
    
    # Iterate through all charts in the sheet
    for chart in sheet._charts:
        titlec = chart.title.text.rich.p[0].text[0].value
        print(titlec)