[python-win32] Fwd: Renaming Excel Spreadsheets

Waldemar Osuch waldemar.osuch at gmail.com
Wed Aug 27 04:59:30 CEST 2008


On Tue, Aug 26, 2008 at 12:57 PM, James Matthews <nytrokiss at gmail.com> wrote:
>
>
> ---------- Forwarded message ----------
> From: Greg Lindstrom <gslindstrom at gmail.com>
> Date: Tue, Aug 26, 2008 at 11:54 AM
> Subject: Renaming Excel Spreadsheets
> To: python-list at python.org
>
>
> Hello,
>
> I am working with Python to create Excel spreadsheets and have run into a
> couple of problems I hope you can help me with.
>
> First...are there any bindings/libraries into Open Office?
>
> Now, back to Excel.
>
> --> Does anyone know a way to create N worksheets?  By default, 3 are
> created, but I would like more.
>
> --> Is it possible to rename a worksheet inside of the workbook (change
> "Sheet1" to "July 08", for example).
>
> I've been working with Mark Hammond's book on Windows programming with
> Python, but these have me stumped.
>
> Better yet, an Open Source reporting system (ala Crystal Reports) would keep
> me from having to write this.  I've looked at Jasper and DataVision;
>
> Thanks,
> --greg
>

That should get you started

>>> from win32com.client import Dispatch
>>> app = Dispatch('Excel.Application')
>>> app.Visible = True
>>> wrk = app.Workbooks.Add()
>>> wrk.Sheets.Count
3
>>> sh = wrk.Sheets.Add()
>>> wrk.Sheets.Count
4
>>> sh.Name
u'Sheet4'
>>> sh.Name = 'New Name 4'
>>> wrk.Sheets[0].Name
u'New Name 4'
>>> wrk.Sheets[2].Name
u'Sheet2'
>>> wrk.Sheets[2].Name = 'Hello'
>>> wrk.Save()
>>> wrk.Close(0)
>>> app.Quit()
>>>

But I would recommend using xlwt (friendly fork of pyExcelerator) for
Excel generation.
It lets you bypass having to install Microsoft Office.
https://secure.simplistix.co.uk/svn/xlwt/trunk/README.html

The OpenOffice comes with its own version of python that can be used
for automation.
http://wiki.services.openoffice.org/wiki/Python

Another option is to manipulate the OpenOffice documents directly.
http://openoffice-python.origo.ethz.ch/

Or create OpenOffice document as a template with all the formatting you desire
and generate new document by judicious modification of the template's XML.
The OpenOffice documents are just a bunch of XML files zipped together.
Change the extension to *.zip and use your favorite unzipping program
to see for yourself.

Waldemar


More information about the python-win32 mailing list