简体   繁体   English

如何使用Python和Openpyxl将高级样式添加到Excel折线图中

[英]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. 我用红色圆圈圈出了我想添加的图表的功能。

  1. Dashed rectangular drop lines from each data point extending to the X axis of the graph. 从每个数据点延伸到图形的X轴的虚线矩形放线。

  2. 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”设置了单独的字体)

  3. 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.

相关问题 如何使用python的openpyxl在Excel上绘制带有日期格式的折线图 - how to draw line chart with date fomat on excel using openpyxl of python 如何使用Python和openpyxl复制Excel行并过滤值? - How can I copy Excel rows, filtering for a value, with Python and openpyxl? 如何使用 openpyxl 从 excel 文件制作 python 字典? - how can I make a python dictionary from excel file using openpyxl? Python:如何使用反馈循环通过 openpyxl 删除 excel 条目? - Python: How can I remove excel entries via openpyxl using a feedback loop? openpyxl:使用 Python 创建 Excel 工作表时,如何使用具有数据验证的列表? - openpyxl: How can I use lists with Data Validation when creating Excel worksheets using Python? 如何使用 Python 和 openpyxl 替换 Excel 工作表中的 HTML 代码? - How can I replace HTML code inside an excel sheet using Python and openpyxl? 如何使用 openpyxl 库将列从一张 excel 表复制到另一张 python? - How can I copy columns from one excel sheet to another with python using openpyxl Library? 如何使用 openpyxl 将 csv 文件写入 Excel 工作表? - How can i write a csv file to an excel sheet using openpyxl? 有没有办法使用 openpyxl 在 Python 中删除或修改 xlsx (Excel) 图表? - Is there a way to remove or modify a xlsx (Excel) chart in Python using openpyxl? 如何使用 python 将 excel 图表保存为图像? - How can I save an excel chart as an image using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM