如何使用openpyxl设置图表区域的图形属性 [英] How to set up the Graphical Properties of Chart Area using openpyxl

查看:514
本文介绍了如何使用openpyxl设置图表区域的图形属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改Chart_area的背景颜色并删除所有边框.关于背景颜色,我尝试了建议的解决方案:

I would like to change the background color of Chart_area as well as remove all borders. Regarding the background color, I have tried the solution suggested: How to set Background Color of Plot Area of Chart using openpyxl but this solution doesn't work for any charts i.e. BarChart, AreaChart, LineChart, ScatterChart, RadarChart etc... I tried two openpyxl versions: 2.4.7, 2.4.9. without success.

# 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-是否可以从图表中删除边框? .但是我不明白如何处理该库.没有一种更简单的方法令我感到惊讶.

I also tried to remove the borders of chart_area. I found this post: openpyxl - Ability to remove border from charts?. But I don't understand how do I handle to modify the library. I am a bit surprised that there is no simpler way.

在此先感谢您的帮助.

Many thanks in advance for your help.

推荐答案

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

This process may be helpful guidance to find an answer.

  1. 打开一个新的Excel电子表格并使用创建一个感兴趣的图表 默认设置.用文件名保存 默认图表由excel.xlsx保存".
  2. 现在通过将背景色更改为紫色(十六进制)来修改图表 代码"AA00AA").用文件名保存 "redBG-chart-saved-by-excel.xlsx"并关闭excel.
  3. 使用zip程序(7zip或类似文件)打开默认的excel文件 并将xl>图表> chart1.xml文件解压缩到您的工作中 文件夹.将其重命名为"default-chart-saved-by-excel.xml". "redBG"案.
  4. 使用所选的XML编辑器打开两个xml文件(某些Web 浏览器运行良好),并查找具有以下内容的xml代码块: 通过搜索紫色的十六进制代码'AA00AA'进行更改.该块就是您希望openpyxl定位的目标.
  1. Open a new excel spreadsheet and create a chart of interest with default settings. Save this with filename "default-chart-saved-by-excel.xlsx".
  2. Now modify the chart by changing the background color to purple (hex code 'AA00AA'). Save this with filename "redBG-chart-saved-by-excel.xlsx" and close excel.
  3. Open the default excel file using a zip program (7zip or similar) and extract the xl > charts > chart1.xml file to your working folder. Rename it to "default-chart-saved-by-excel.xml" Repeat for the "redBG" case.
  4. Open both of the xml files with an XML editor of choice (some web browsers work well) and look for the chunk of xml code that has changed by searching for the purple hex code 'AA00AA' That chunk is what you want openpyxl to target.

这篇关于如何使用openpyxl设置图表区域的图形属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆