[英]How can I add advanced styles to my Excel Line Chart Using Python & Openpyxl
Below is the code and the resulting Excel chart generated using Python's Openpyxl Package. 以下是使用Python的Openpyxl软件包生成的代码和生成的Excel图表。
Also below is the chart I would like the result to look like How can I add the following things to the graph? 另外,下面是图表,我希望结果看起来像如何将以下内容添加到图表中? I've circled in red the features of the graph I wanted to add.
我用红色圆圈圈出了我想添加的图表的功能。
Dashed rectangular drop lines from each data point extending to the X axis of the graph. 从每个数据点延伸到图形的X轴的虚线矩形放线。
Left justified title alignment with 2 separate fonts/sizes (1 font set for "Title", seperate font set for "Title2" & "Title3") 左对齐标题对齐方式,带有2种单独的字体/大小(“ Title”设置了1种字体,“ Title2”和“ Title3”设置了单独的字体)
Multiple Floating text boxes below the graphs (See ***Notes1,2,3 at the bottom) 图形下方的多个浮动文本框(请参见底部的***注1、2、3)
Code 码
import openpyxl
from openpyxl.chart import LineChart,Reference
from openpyxl.chart.label import DataLabelList
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font
excel = "Template.xlsx"
excel_file = openpyxl.load_workbook(excel)
Output_Sheet = excel_file.get_sheet_by_name('Sheet1')
excel_file.save("Template2.xlsx")
#Define data
data = Reference(Output_Sheet, min_col = 25, min_row = 5,
max_col = 25, max_row = 15)
#Define category
categories = Reference(Output_Sheet, min_col = 23, min_row = 5,
max_col = 24, max_row = 15)
#Initialize
chart = LineChart()
#add Data
chart.add_data(data)
#Add category
chart.set_categories(categories)
#Define title
chart.title = " Title \n Title2 \n Title3"
# set the title of the x-axis
# set the title of the y-axis
chart.y_axis.title = "Revenue "
#Define chart and font
font_test = Font(typeface='Avenir LT Std 55 Roman')
cp = CharacterProperties(latin=font_test, sz=1500)
chart.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
chart.y_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
chart.title.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
#Size
chart.height = 17.5 # default is 7.5
chart.width = 40 # default is 15
#Remove legend
chart.legend = None
s2 = chart.series[0]
s2.smooth = True # Make the line smooth
#Show Data Label
chart.dataLabels = DataLabelList()
chart.dataLabels.showVal = True
#Define font and size
font_test = Font(typeface='Avenir LT Std 55 Roman')
cp = CharacterProperties(latin=font_test, sz=750)
chart.dataLabels.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)])
#Add Chart
Output_Sheet.add_chart(chart, "B18")
# save the file
excel_file.save("Template2.xlsx")
Current output 电流输出
Desired Output 期望的输出
For this kind of effect you must be prepared to analyse the XML of the file. 为此,您必须准备分析文件的XML。 For example, you can create the styled title using RichText objects, much in the way you do with the data labels and it looks like you need to use drop lines on the chart something like
chart.dropLines = ChartLines()
例如,您可以使用RichText对象创建样式化的标题,这与处理数据标签的方式非常相似,看起来您需要在图表上使用
chart.dropLines = ChartLines()
线,如chart.dropLines = ChartLines()
Because openpyxl implements the OOXML specification almost directly, the best documentation for this is the OOXML specification. 因为openpyxl几乎直接实现OOXML规范,所以最好的文档就是OOXML规范。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.