Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用openpyxl将数据动态添加到现有图表中_Python_Excel_Charts_Openpyxl - Fatal编程技术网

Python 如何使用openpyxl将数据动态添加到现有图表中

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

当使用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 = 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")