Skip to main content
 首页 » 编程设计

python之xlsxwriter 图表创建动态行

2025年01月19日8虾米哥

我正在尝试使用 xlsxwriter python 模块创建图表。 它工作正常,但我不想硬编码行数 此示例将绘制 30 行图表。

chart.add_series({ 
   'name':   'SNR of old AP', 
   'values': '=Depart!$D$2:$D$30', 
   'marker': {'type': 'circle'}, 
   'data_labels': {'value': True,'num_format':'#,##0'}, 
}) 

对于值':我希望行数是动态的。我该怎么做呢? 谢谢。

请您参考如下方法:

It works fine, but I would like to not have to hard code the row amount

XlsxWriter 在 add_series() 中支持针对这种情况的列表语法。所以你的例子可以写成:

chart.add_series({ 
   'name':   'SNR of old AP', 
   'values': ['Depart', 1, 3, 29, 3], 
   'marker': {'type': 'circle'}, 
   'data_labels': {'value': True, 'num_format':'#,##0'}, 
}) 

然后您可以以编程方式设置任何 first_row、first_col、last_row、last_col 参数。

请参阅 add_series() 的文档.