python xlwings chart模块各种问题今天都遇到了

问题1、增加图表,按网上搜索的设置图表标题一直报错(pywintypes.com_error: (-2147352567, ‘发生意外。’, (0, ‘S’, ‘ 该对象无标题。’, None, 0, -2147024809), None))

问题2、标签问题位置调整,标签默认在图表右侧

问题3、遇到具体问题的处理方法,比如:msoElementChartTitleAboveChart 双面 在图表上方显示标题。这个参数的枚举值显示为双面,要换成具体的int型数字的方法。

下面列举具体的问题原因及解决方法:

1 # generate chat
2         chart = sht.charts.add(500, 10)
3         chart.set_source_data(sht.range("B7:E8"))
4         chart.chart_type = 'pie_exploded'
5         chart.api[1].ChartTitle.Text = 'TestReport'  # change the title name

这部分代码跟网上找到的都一样,比如这个篇:https://blog.csdn.net/weixin_42146296/article/details/103647940,但很纳闷运行这段代码正常,运行我自己的就报错,然后把生成的图表手动去操作时发现标题选项未勾选,代码默认生成的报告就不包含标题对象,此时再去看原来的报错,已经明确提示了”该对象无标题”,如问题1中后面红色字体加粗部分,只怪当时也看不懂这句什么意思,一步一步尝试才发现这个坑

python xlwings chart模块各种问题今天都遇到了

那接下来首选就是要让这个标题通过代码使能,并进行显示出来,然后才能去改标题,使用excel的录抽宏功能,就能很正常的知道使用api的哪个属性了,录制宏过程请看前一篇文章,此处跳过,直接列出宏

python xlwings chart模块各种问题今天都遇到了

修改代码如下:

1 # generate chat
2         chart = sht.charts.add(500, 10)
3         chart.set_source_data(sht.range("B7:E8"))
4         chart.chart_type = 'pie_exploded'
5         chart.api[1].SetElement('msoElementChartTitleAboveChart')  # enable the title show up
6         chart.api[1].ChartTitle.Text = 'TestReport'  # change the title name

报错如下:报错的意思很明显是整型的枚举值

csharp;gutter:true; return self.<em>oleobj</em>.InvokeTypes(2502, LCID, 1, (24, 0), ((3, 1),),Element ValueError: invalid literal for int() with base 10: 'msoElementChartTitleAboveChart'</p> <pre><code> 通过百度搜索关键字:msoElementChartTitleAboveChart,无语了微软官网上也显示有问题,竟然显示"双面",这是什么意思,别急,继续往下看,这个文档错误还能把人憋死不成 ![python xlwings chart模块各种问题今天都遇到了](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230619/1430442-20201103155625918-1912836826.png) 继续查看前面所录制的宏,由于通过网页查找到官网的也显示有错误,只能再重新返回VBA去找办法,光标放至msoElementChartTitleAboveChart并右键,选择【快速信息】,显示的值为2 ![python xlwings chart模块各种问题今天都遇到了](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20230619/1430442-20201103155926783-1749266726.png) 所以重新修改代码如下: </code></pre> <p>1 # generate chat 2 chart = sht.charts.add(500, 10) 3 chart.set_source_data(sht.range("B7:E8")) 4 chart.chart_type = 'pie_exploded' 5 chart.api[1].SetElement(2) # enable the title show up 6 chart.api[1].ChartTitle.Text = 'TestReport' # change the title name

生成的图表如下:PERFECT

python xlwings chart模块各种问题今天都遇到了

下面列出几个给我启发的文档,应该是帮了我很大的忙:

1、python操作xlwings在excel中制作图

2、使用xlwings 的API接口属性方法,官方文档链接

3、SetElement对应的详细枚举值

Original: https://www.cnblogs.com/aziji/p/13920938.html
Author: iSZ
Title: python xlwings chart模块各种问题今天都遇到了

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/684734/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球