Python – Extra Excel graphique série avec win32com

J'écris un code pour une affectation, et je dois créer un tableau de colonnes simple dans Excel. Cet après-midi, j'ai trouvé win32com (des outils étonnants en passant), mais je souffre soit du manque de documentation à ce sujet, soit de mon manque de chance de le find ^^

Je joue avec les charts, et je pense que j'ai réussi à faire ce que je veux, avec une petite exception: la fonction que j'ai écrite crée toujours 2 séries de colonnes.

C'est ce que j'ai:

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 

Je crée une nouvelle série dans le tableau, et elle contient toutes les informations dont j'ai besoin. Cependant, lorsque j'exécute le script, je finis par un graphique Excel avec 4 colonnes, avec la même information (par paires). J'ai fait tout ce que je peux, mais je ne peux pas find ce qui crée cette deuxième série de valeurs sur l'axe X …

J'apprécie grandement toute aide ^^ Merci!

En observant le comportement d'logging d'une macro dans Excel qui effectue les mêmes actions que la réplication en Python, nous pouvons voir qu'il ne semble pas nécessaire de créer une nouvelle série telle que

 series = chart.SeriesCollection().NewSeries() 

J'ai pu simplement faire reference à une série existante

 series = chart.SeriesCollection(1) 

Le code suivant semble me donner le comportement souhaité sur mon ordinateur.

 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 import win32com.client 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 xlApp = win32com.client.Dispatch ('Excel.Application') 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 xlBook = xlApp.Workbooks.Add () 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 xlSheet = xlBook.Sheets (1) 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 xlSheet.Name = "Algoritmos de Busqueda" 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 xlSheet.Cells (1,1) .Value = "Secuencial" 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 xlSheet.Cells (2,1) .Value = "Binaria" 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 xlSheet.Cells (1,2) .Value = "32" 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 xlSheet.Cells (2,2) .Value = "32" 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 chart = xlApp.Charts.Add () 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 chart.Name = "Grafico" + xlSheet.Name 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 série = chart.SeriesCollection (1) 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 series.XValues ​​= xlSheet.Range ("A1: A2") 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 series.Values ​​= xlSheet.Range ("B1: B2") 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 series.Name = "Algoritmos" 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 

Cela a été simplifié au ssortingct minimum. J'ai testé ceci dans Python 2.7 avec Excel 2003.