简体   繁体   中英

Is there a way to remove or modify a xlsx (Excel) chart in Python using openpyxl?

I can't find a way to modify or remove an existing Excel graph in an existing workbook.

That's the only code I could archieve:

from openpyxl.chart import AreaChart, Reference, Series
import openpyxl as opyxl

def CreateGraphAC(wb, ws, rows, columns):
    chart = AreaChart()
    chart.title = "Area Chart"
    chart.style = 13
    chart.x_axis.title = 'Test'
    chart.y_axis.title = 'Percentage'

    cats = Reference(ws, min_col=1, min_row=2, max_row=rows)
    data = Reference(ws, min_col=2, min_row=1, max_col=columns, max_row=rows)

    chart.add_data(data, titles_from_data=True)
    chart.set_categories(cats)

    ws.add_chart(chart, "F1")
    wb.save('test.xlsx')

wb = opyxl.load_workbook('test.xlsx')
allSheetNames = wb.sheetnames
ws = wb.active
CreateGraphAC(wb, ws, 2, 2)

The only thing I discovered is that you can get the list of graphs on a worksheet with:

ws._charts

Any suggestions? Thank you all in advance.

Solved by myself

Use del worksheet._charts[number] with a workbook.save('file.xlsx') and create a new chart with modified parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM