我使用openpyxl在excel中创建图表,我希望我的一些图表的y轴单位为百分比。这在openpyxl中是否可行?它在xlsxwriter中非常简单,但我无法弄清楚如何在openpyxl中完成它。下面的代码用于包含两个y轴的图表。
谢谢!
修改
事实证明,就像更改y_axis的格式一样简单,至少对于这个问题。
c2.y_axis.number_format = '0%'
def line_chart_2axis(sheet_name, file_name):
font = Font(color = "000000", size = 8.5, name = 'Avenir Next Condensed Regular')
c1 = LineChart()
data1 = Reference(data_sheet, min_col=3, min_row=1, max_col=3, max_row=len(df1)+1)
dates = Reference(data_sheet, min_col=1, min_row=2, max_col=1, max_row=len(df1)+1)
c1.add_data(data1, titles_from_data=True)
c1.title = "Title"
c1.style = 5
c1.y_axis.title = 'Y Axis 1'
c1.x_axis.number_format = 'Mmm-YYYY'
c1.set_categories(dates)
c1.y_axis.majorGridlines = None
c1.legend.position = 'b'
s1 = c1.series[0]
s1.graphicalProperties.line.width = 14000
s1.graphicalProperties.line.solidFill = "8989ff"
c2 = LineChart()
data2 = Reference(data_sheet, min_col=2, min_row=1, max_col=2, max_row=len(df1)+1)
c2.add_data(data2, titles_from_data=True)
c2.y_axis.axId = 200
c2.y_axis.title = "Y Axis 2"
c2.y_axis.crosses = "max"
c2.y_axis.majorGridlines = None
s2 = c2.series[0]
s2.graphicalProperties.line.width = 14000
s2.graphicalProperties.line.solidFill = "000062"
c1 += c2
sheet.add_chart(c1, "B2")