[Python] How do I programmatically (via Python/xlWings) add axes to an existing chart?

Discussão em 'Python' iniciado por Stack, Fevereiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I have a very odd problem.

    I have a python script that adds data every week to an existing Excel spreadsheet to track a weekly metric I'm interested in. When it pastes the new data in the existing worksheet, the chart that tracks the data loses its horizontal and vertical axes (probably some Excel or Pandas.ExcelWriter issue; would love to solve that, but that's not the purpose of this post).

    I'm trying to have the python script re-add the missing axes, but can't find anything in the xlwings .API that will read it. I can add titles, text, etc.. but not sure how to actually add the axes back.

    I thought maybe tickling the axes would cause them to reappear, such as:

    sheet = book.sheets['Criticality_Over_Time']
    chart = sheet.charts[0]
    chart.api[1].Axes(xl.constants.AxisType.xlCategory, xl.constants.AxisGroup.xlPrimary).MajorUnitIsAuto = True
    chart.api[1].Axes(xl.constants.AxisType.xlCategory, xl.constants.AxisGroup.xlPrimary).MinorUnitIsAuto = True


    Is there a way to re-add the axes? I've searched everywhere and can't find a good example.

    Continue reading...

Compartilhe esta Página