Python - 带有 win32com 的额外 Excel 图表系列 [英] Python - Extra Excel chart series with win32com

查看:42
本文介绍了Python - 带有 win32com 的额外 Excel 图表系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为作业编写一些代码,我需要在 Excel 中创建一个简单的柱状图.今天下午我找到了 win32com(顺便说一句,这是一个了不起的工具),但我一直在苦于要么缺乏关于它的文档,要么我没有找到它 ^^

I'm writing some code for an assignment, and I need to create a simple column chart in Excel. This afternoon I found win32com (amazing tool by the way), but I've been suffering from either the lack of documentation about it, or my lack of luck to find it ^^

我在玩图表,我想我已经设法做我想做的事,除了一个小例外:我编写的函数总是创建 2 个系列的列.

I'm playing around with the charts, and I think I've managed to do what I want, with a little exception: the function I wrote always creates 2 series of columns.

这就是我所拥有的:

xlBook = xlApp.Workbooks.Add()

xlSheet = xlBook.Sheets(1)
xlSheet.Name = "Algoritmos de Busqueda"
xlSheet.Cells(1,1).Value="Secuencial"
xlSheet.Cells(2,1).Value="Binaria"
xlSheet.Cells(1,2).Value="32"
xlSheet.Cells(2,2).Value="32"

chart = xlApp.Charts.Add()
chart.Name= "Grafico "+xlSheet.Name
series = chart.SeriesCollection().NewSeries()
valoresx=xlSheet.Range("A1:A2")
valoresy=xlSheet.Range("B1:B2")
series.XValues= valoresx
series.Values= valoresy
series.Name= "Algoritmos"
xAxis= chart.Axes()[0]
yAxis= chart.Axes()[1]
xAxis.HasMajorGridlines = True
yAxis.HasMajorGridlines = True

我在图表内创建了一个新系列,其中包含我需要的所有信息.但是,当我运行脚本时,我最终得到了一个包含 4 列的 Excel 图表,具有相同的信息(成对).我已经做了我能做的一切,但我就是找不到是什么在 X 轴上创建了第二个系列的值...

I create a new series inside the chart, and it contains all the info I need. However, when I run the script, I end up with an Excel chart with 4 columns, with the same info (in pairs). I've done everything I can, but I just can't find what's creating this second series of values on the X axis...

我非常感谢任何帮助^^谢谢!

I greatly appreciate any help ^^ Thanks!

推荐答案

通过观察在 Excel 中录制宏的行为,该宏执行与我们在 Python 中复制的操作相同的操作,我们可以看到似乎不需要创建一个新系列,例如

By observing the behavior of recording a macro in Excel which performs the same actions as we are replication in Python we can see that there does not appear to be a need to create a new series such as

series = chart.SeriesCollection().NewSeries()

我能够简单地引用现有系列

I was able to simply reference an existing series

series = chart.SeriesCollection(1)

以下代码似乎在我的计算机上为我提供了所需的行为.

The following code seems to give me the desired behavior on my computer.

import win32com.client
xlApp = win32com.client.Dispatch('Excel.Application')

xlBook = xlApp.Workbooks.Add()

xlSheet = xlBook.Sheets(1)
xlSheet.Name = "Algoritmos de Busqueda"
xlSheet.Cells(1,1).Value="Secuencial"
xlSheet.Cells(2,1).Value="Binaria"
xlSheet.Cells(1,2).Value="32"
xlSheet.Cells(2,2).Value="32"

chart = xlApp.Charts.Add()
chart.Name= "Grafico "+xlSheet.Name
series = chart.SeriesCollection(1)
series.XValues= xlSheet.Range("A1:A2")
series.Values= xlSheet.Range("B1:B2")
series.Name= "Algoritmos"
chart.Axes()[0].HasMajorGridlines = True

这已被简化到最低限度.我在 Python 2.7 和 Excel 2003 中对此进行了测试.

This has been simplified to the bare minimum. I tested this in Python 2.7 with Excel 2003.

这篇关于Python - 带有 win32com 的额外 Excel 图表系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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