473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Excel 2007 Charts with PyWin32

Hi all,

I’m looking to plot charts in Excel from python. After some Googling
I’ve found the following code:

def plot(x, y, xAxisLog=False, yAxisLog=False):
# acquire application object, which may start application
application = Dispatch("Excel.Application")

# create new file ('Workbook' in Excel-vocabulary)
workbook = application.Workbooks.Add()

# store default worksheet object so we can delete it later
defaultWorksheet = workbook.Worksheets(1)

# build new chart (on seperate page in workbook)
chart = workbook.Charts.Add()

print "chart", chart
chart.ChartType = constants.xlXYScatter
chart.Name = "Plot"

# create data worksheet
worksheet = workbook.Worksheets.Add()
worksheet.Name = "Plot data"

# install data
xColumn = addDataColumn(worksheet, 0, x)
yColumn = addDataColumn(worksheet, 1, y)

# create series for chart
series = chart.SeriesCollection().NewSeries()
series.XValues = xColumn
series.Values = yColumn
series.Name = "Data"
series.MarkerSize = 3

# setup axises
xAxis = chart.Axes()[0]
yAxis = chart.Axes()[1]
xAxis.HasMajorGridlines = True
yAxis.HasMajorGridlines = True
if xAxisLog:
xAxis.ScaleType = constants.xlLogarithmic
if yAxisLog:
yAxis.ScaleType = constants.xlLogarithmic

# remove default worksheet
defaultWorksheet.Delete()

# make stuff visible now.
chart.Activate()
application.Visible = True

def genExcelName(row, col):
"""Translate (0,0) into "A1"."""
if col < 26:
colName = chr(col + ord('A'))
else:
colName = chr((col / 26)-1 + ord('A')) + \
chr((col % 26) + ord('A'))
return "%s%s" % (colName, row + 1)

def addDataColumn(worksheet, columnIdx, data):
range = worksheet.Range("%s:%s" % (
genExcelName(0, columnIdx),
genExcelName(len(data) - 1, columnIdx),
))
for idx, cell in enumerate(range):
cell.Value = data[idx]
return range

# A simple example:
plot( (1,2,3,4,5), (6,7,8,9,10) )

I’m continually getting errors with:

chart.ChartType = constants.xlXYScatter

with

AttributeError: xlXYScatter

If I try other chart types, such as xl3DPieExploded, I still receive
the same error. Has anyone got any suggestions as to why it can’t seem
to find any chart types? I’m running WinXP, Python 2.5, latest PyWin32
and Excel 2007.

I don’t know who the original author of the above code is, it
defiantly wasn’t me, so major thanks to whoever it was.

Oct 14 '08 #1
1 8700
Solved. See http://bytes.com/forum/thread482449.html

application = Dispatch("Excel.Application")

should be

application =
win32com.client.gencache.EnsureDispatch('Excel.App lication')
Oct 14 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael Jordan | last post by:
I'm hoping that someone here can give me some insight into a problem I'm running into with Python, pywin32 and Excel. All-in-all using Python and pywin32 is great but I've run into a strange...
3
by: ian castleden | last post by:
From time to time I see requests for some sort of python MS Excel interface/software. I have written just such software 100% python (works with Jython too! at least it did a year ago). It was...
0
by: mscir | last post by:
Date: Thu, 08 Jul 2004 17:02:27 -0700 Organization: Posted via Supernews, http://www.supernews.com Message-ID: <10ero4l3kp2qa65@corp.supernews.com> Reply-To: mscir@usa.com User-Agent: Mozilla/5.0...
10
by: Mark Day | last post by:
Hi All, I am using Access 2000 to generate over 40 Excel charts and pivot tables using early binding to the Excel 9.0 object library. I am finding that if I show the Excel object while processing...
0
by: Rach | last post by:
Hi, Does anyone know how to export charts/graphs from EXCEl into Powerpoint-Microsoft Charts though VBA code. I would like to import the underlying data with the chart as well. I know we can...
5
by: Takeadoe | last post by:
I'm doubtful that the group is unbiased, but I will ask anyhow :o). If one intends to plot lots of data, primarily XY line and scatter plots, and mass produce them (in many cases I will be doing...
3
by: saragoesnuts | last post by:
I have a program that automates to Excel with information in tables. I want to add charts to the excel file (pie charts, bar charts, etc). Is this possible?
4
by: Fred | last post by:
Is it possible to generate a chart using the excel 2007 engine for a web page? (output in jpg)... If not what 3rd party solution would you recommend for this purpose Thanks, Fred
9
by: AGP | last post by:
I have a VB6 application that has an OLE container with an embedded Excel chart. Our office updated to Excel 2007 and now although the app works the charts looks like crap. Since the app is fairly...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.