Python - 带有 win32com 的额外 Excel 图表系列
我正在为作业编写一些代码,并且需要在 Excel 中创建一个简单的柱形图。今天下午,我发现了 win32com(顺便说一句,这是一个很棒的工具),但我一直在苦于缺乏有关它的文档,或者我缺乏找到它的运气 ^^
我正在摆弄图表,我想我已经成功地做到了我想要的,但有一点例外:我编写的函数总是创建 2 个系列的列。
这就是我所得到的:
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'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 ^^
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.
This is what I've got:
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
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过观察在 Excel 中记录宏的行为,该宏执行与我们在 Python 中复制相同的操作,我们可以看到似乎不需要创建一个新系列,例如
我能够简单地引用现有系列
。以下代码似乎在我的计算机上给了我所需的行为。
这已被简化到最低限度。
我在 Python 2.7 和 Excel 2003 中对此进行了测试。
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
I was able to simply reference an existing series
The following code seems to give me the desired behavior on my computer.
This has been simplified to the bare minimum.
I tested this in Python 2.7 with Excel 2003.