我正在尝试为我的工作自动设置图表格式。很多时候,我们必须制作 50 多个具有特定样式的图表。与图表的唯一区别是轴标题,必须手动编辑以供客户理解。我已经编写了一个代码,将图表格式化为确切的格式。唯一的问题是我不知道如何仅指定格式而不指定轴标题应该是什么。我的代码正确的是这样的。
import openpyxl as opyxl
from openpyxl.styles import Font
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font, RegularTextRun
from openpyxl.xml.functions import fromstring
from openpyxl.chart.shapes import GraphicalProperties
#The Point of This Program is to create a cleaner program
def graph_formatting(file, font_style = 'Times New Roman', fill_color = "000000", xt_name = "Default", yt_name = "Default", font_size = 1000, bold = '0'):
wb = opyxl.load_workbook(file)
for sheets in wb.chartsheets: #loops through available chartsheets
dechart = sheets._charts[0]
#This specifies the axis format using XML;
xml = f"""
<txPr>
<a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:r>
<a:rPr b="{bold}" i="0" sz="{font_size}" spc="-1" strike="noStrike">
<a:solidFill>
<a:srgbClr val="{fill_color}" />
</a:solidFill>
<a:latin typeface="{font_style}" />
</a:rPr>
<a:t>{yt_name}</a:t>
</a:r>
</a:p>
</txPr>
"""
dechart.y_axis.title.tx.rich = RichText.from_tree(fromstring(xml)) #Change the format using specified XML;
#Same idea
xml = f"""
<txPr>
<a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:r>
<a:rPr b="{bold}" i="0" sz="{font_size}" spc="-1" strike="noStrike">
<a:solidFill>
<a:srgbClr val="{fill_color}" />
</a:solidFill>
<a:latin typeface="{font_style}" />
</a:rPr>
<a:t>{xt_name}</a:t>
</a:r>
</a:p>
</txPr>
"""
dechart.x_axis.title.tx.rich = RichText.from_tree(fromstring(xml))
#Changes rest of the of charts, uch as axis labels
font = Font(typeface=font_style) #set the font
size = font_size #set the font size
cp = CharacterProperties(latin=font, sz=size, b=False)
pp = ParagraphProperties(defRPr=cp)
dechart.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)]) #changes the x axis label font
dechart.y_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)]) #changes the y axis label font
#chart styling for the minor details
dechart.graphical_properties = GraphicalProperties()
dechart.graphical_properties.line.noFill = True
dechart.graphical_properties.line.prstDash = None
dechart.x_axis.graphicalProperties.line.solidFill = fill_color #changes the line color
dechart.y_axis.graphicalProperties.line.noFill=False #draws the y axis line
dechart.y_axis.majorGridlines = None #gets rid of gridline
dechart.y_axis.majorTickMark = 'out' #create tickmarks for x and y axis
dechart.x_axis.majorTickMark = 'out'
#wb.save(path)
if dechart:
wb.save(file)
它在大多数情况下都有效。我只想保持现有的轴标题不变。例如,如果轴标题显示“客户百分比”,那么在运行程序后它仍应显示“客户百分比”,而不是“默认”。我尝试删除该行
<a:t>{yt_name}</a:t>
在 XML 中,但现在它只是删除轴标题。我尝试查看 openpyxl 文档,但找不到解决方案。
该程序的理想工作流程是:
问题是当我第二次使用该程序时,旧图表中的旧轴标题将被“默认”覆盖
您的意思是抓取原始标题并在格式化后重置它们
...
for sheets in wb.chartsheets: #loops through available chartsheets
dechart = sheets._charts[0]
orig_x_title = dechart.x_axis.title.text.rich.paragraphs[0].text[0].value
orig_y_title = dechart.y_axis.title.text.rich.paragraphs[0].text[0].value
print(f"Original X AXIS Title text '{orig_x_title}'")
print(f"Original Y AXIS Title text '{orig_y_title}'")
#This specifies the axis format using XML;
xml = f"""
<txPr>
<a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:r>
<a:rPr b="{bold}" i="0" sz="{font_size}" spc="-1" strike="noStrike">
<a:solidFill>
<a:srgbClr val="{fill_color}" />
</a:solidFill>
<a:latin typeface="{font_style}" />
</a:rPr>
<a:t>{yt_name}</a:t>
</a:r>
</a:p>
</txPr>
"""
dechart.y_axis.title.tx.rich = RichText.from_tree(fromstring(xml)) #Change the format using specified XML;
#Same idea
xml = f"""
<txPr>
<a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
<a:r>
<a:rPr b="{bold}" i="0" sz="{font_size}" spc="-1" strike="noStrike">
<a:solidFill>
<a:srgbClr val="{fill_color}" />
</a:solidFill>
<a:latin typeface="{font_style}" />
</a:rPr>
<a:t>{xt_name}</a:t>
</a:r>
</a:p>
</txPr>
"""
#dechart.x_axis.title.tx.rich = RichText.from_tree(fromstring(xml))
### Reset Axis Titles text to orig
dechart.x_axis.title = orig_x_title
dechart.y_axis.title = orig_y_title
#Changes rest of the of charts, uch as axis labels
font = Font(typeface=font_style) #set the font
size = font_size #set the font size
...