python-如何使用openpyxl设置图表区域的图形属性

我想更改Chart_area的背景颜色以及删除所有边框.关于背景色,我尝试了建议的解决方案:How to set Background Color of Plot Area of Chart using openpyxl,但该解决方案不适用于任何图表,例如BarChart,AreaChart,LineChart,ScatterChart,RadarChart等.我尝试了两个openpyxl版本:2.4.7、2.4.9 .没有成功.

# setup the chart
chart = LineChart()
# test to remove border line 
*****************************************************
chart.graphical_properties = 
GraphicalProperties(ln=LineProperties(noFill=True))
chart.height = 9
chart.width = 13.4
props = GraphicalProperties(solidFill="f2f2f2")

# setup and append the first series
list_names = ['Fund', 'Market', 'Benchmark']
data = Reference(ws2, min_col=2, min_row=1, max_col=n + 1, max_row=p + 
1)

chart.x_axis.number_format = 'mmm yy'
chart.x_axis.majorTimeUnit = "months"
chart.add_data(data, titles_from_data=True)
dates = Reference(ws2, min_col=1, min_row=2, max_col=1, max_row=1 + p)
chart.set_categories(dates)
# Style the lines
chart.series[0].graphicalProperties.line.solidFill = "7E3F00"
chart.series[1].graphicalProperties.line.solidFill = "45788C"
chart.series[2].graphicalProperties.line.solidFill = "8BADD9"
chart.plot_area.graphicalProperties = props
# test to change background color 
*****************************************************
chart.graphical_properties = props
ws1.add_chart(chart, "B14") 

我还试图删除chart_area的边框.我发现了这篇文章:openpyxl – Ability to remove border from charts?.但是我不明白如何处理该库.我对没有更简单的方法感到惊讶.

在此先感谢您的帮助.

解决方法:

此过程可能是寻找答案的有用指导.

>打开一个新的Excel电子表格,并使用
默认设置.用文件名保存
“默认图表保存的逐excel.xlsx”.
>现在通过将背景颜色更改为紫色(十六进制)来修改图表
代码“ AA00AA”).用文件名保存
“ redBG-chart-saved-by-excel.xlsx”并关闭excel.
>使用zip程序(7zip或类似文件)打开默认的excel文件
并提取xl>图表> chart1.xml文件到您的工作
夹.将其重命名为“ default-chart-saved-by-excel.xml”.
“ redBG”案例.
>使用所选的XML编辑器打开两个xml文件(某些Web
浏览器工作良好),并查找具有以下内容的xml代码块:
通过搜索紫色的十六进制代码“ AA00AA”进行更改.该块是您希望openpyxl定位的对象.

上一篇:将数字单元格的单元格属性设置为数字时,将txt转换为xlsx


下一篇:Python使用openpyxl插入excel批注,修改批注