Python 如何使用openpyxl将数据动态添加到现有图表中
当使用openpyxl将新数据输入excel时,我尝试将数据动态添加到现有折线图中。下面是我尝试过的示例代码,但当遇到else部分时,图表中没有任何更改。 谢谢Python 如何使用openpyxl将数据动态添加到现有图表中,python,excel,charts,openpyxl,Python,Excel,Charts,Openpyxl,当使用openpyxl将新数据输入excel时,我尝试将数据动态添加到现有折线图中。下面是我尝试过的示例代码,但当遇到else部分时,图表中没有任何更改。 谢谢 from openpyxl import load_workbook from openpyxl.chart import (LineChart, Reference) wb = load_workbook("Hello.xlsx") sheet = wb.active exists = 0 chart = Lin
from openpyxl import load_workbook
from openpyxl.chart import (LineChart, Reference)
wb = load_workbook("Hello.xlsx")
sheet = wb.active
exists = 0
chart = LineChart()
if exists == 1:
values = Reference(sheet, min_col=3, min_row=1, max_row=sheet.max_row)
categories = Reference(sheet, min_col=1, min_row=2, max_row=sheet.max_row,max_col=1)
chart.add_data(values, titles_from_data=True)
chart.set_categories(categories)
chart.title = "sample"
chart.x_axis.title = "date"
chart.y_axis.title = "followers"
sheet.add_chart(chart, "F2")
else:
values_update = Reference(sheet, min_col=3, min_row=1, max_row=sheet.max_row)
chart.add_data(values_update)
wb.save("Hello.xlsx")