Customized series title in openpyxl python

流过昼夜 提交于 2020-01-07 03:51:14

问题


I am trying to modify the existing xlsx sheet and adding graphs to it using openpyxl module in python.

But while creating a line chart, the series title is shown as Series 1,Series 2,Series 3,Series 4 where as I need to rename the series title as "A", "B", "C", D". (Note: this name are not fetched from any cell)

Another possible solution would to give series name from another worksheet apart from row/column of the same worksheet. But not sure whether it is doable.

Below code give me the series name from row/column of worksheet 'ws'. But I need to rename it (customize name) or assign the series name from another sheet.

Can any one help me on this.?

c1 = LineChart()
c1.title = worksheet
c1.x_axis.title = "Average depth"
c1.y_axis.title = "Average Response time (ms)"
c1.y_axis.majorGridlines = None

refseries1 = Reference(ws, min_col=5, min_row=2, max_col=5, max_row=9)
seriesdata1 = Reference(ws, min_col=6, min_row=10, max_col=6, max_row=15)

c1.add_data(data=seriesdata1,titles_from_data=False)
c1.set_categories(refseries1)

In above code, refseries1 is xaxis data and seriesdata1 is y axis data.


回答1:


I found a solution in a different question that works for me:

from openpyxl.chart import LineChart, Reference, Series

chart1 = LineChart()
# setup and append the first series
values = Reference(dprsheet, min_col=2, min_row=1, max_row=34)
series = Series(values, title="First series of values")
chart1.append(series)

# setup and append the second series
values = Reference(dprsheet, min_col=3, min_row=1, max_row=34)
series = Series(values, title="Second series of values")
chart1.append(series)

#set x-axis
dates = Reference(dprsheet, min_col=2, min_row=1, max_row=34)
chart1.set_categories(dates)

It's a bit more fiddly than the chart.add_data method, I'm going to assume that if you dig around in the code for that method you'll find something a bit more... pythony.



来源:https://stackoverflow.com/questions/44234905/customized-series-title-in-openpyxl-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!