Python - 额外的Excel图表系列与win32com [英] Python - Extra Excel chart series with win32com
问题描述
我正在为一个作业编写一些代码,我需要在Excel中创建一个简单的柱形图。今天下午我发现了win32com(令人惊讶的工具),但我一直苦于缺乏关于它的文档,或者我缺乏运气找到它^^
我玩的图表,我想我已经设法做我想要的,有一点例外:我写的函数总是创建两个系列的列。
这是我得到的:
xlBook = xlApp.Workbooks.Add b
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
.Name =Algoritmos
xAxis = chart.Axes()[0]
yAxis = chart.Axes(1)
xAxis.HasMajorGridlines = True
yAxis.HasMajorGridlines = True
我在图表中创建一个新系列,它包含了我需要的所有信息。然而,当我运行脚本,我最终得到一个Excel图表与4列,具有相同的信息(成对)。我已经做了一切我可以,但我只是不能找到什么是创建第二个系列的值在X轴...
我非常感谢任何帮助^ ^感谢!
通过观察在Excel中录制宏的行为,执行与我们在Python中的复制相同的操作,可以看到似乎没有需要创建一个新的系列,例如
series = chart.SeriesCollection()。 NewSeries()
我能够简单地引用现有的系列
series = chart.SeriesCollection(1)
以下代码似乎在我的计算机上给我所需的行为。
import win32com.client
pre>
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中测试了这个。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!
解决方案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
This has been simplified to the bare minimum. I tested this in Python 2.7 with Excel 2003.
这篇关于Python - 额外的Excel图表系列与win32com的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!