简体   繁体   中英

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

  2. Left justified title alignment with 2 separate fonts/sizes (1 font set for "Title", seperate font set for "Title2" & "Title3")

  3. Multiple Floating text boxes below the graphs (See ***Notes1,2,3 at the bottom)

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. 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()

Because openpyxl implements the OOXML specification almost directly, the best documentation for this is the OOXML specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM