[Tutor] How to Create Webpage with Python

Alan Gauld alan.gauld at btinternet.com
Tue Jul 15 21:46:54 CEST 2014


On 15/07/14 17:48, John Cast wrote:

> 1) How does one automate the save as webpage from python?

It is possible using COM objects and the pywin32 library.
However that's rarely the best way to approach things.
But if you insist...

Here is a short example of opening a FileOpen dialog...

import win32com.client as com
filepath = r"D:\Whatever\You\want"
fileopen = 1  # change to whatever code you need of 1-4
app = com.Dispatch("Excel.Ap[plication")
app.Visible = True
fd - app.FileDialog(fileopen)
fd.InitialFileName = filepath
fd.Title = "Open a file"
if fd.Show() == -1:
    fd.Execute()

I'll let you use your imagination (and the MSDN web site?) to
work out how to do a SaveAs dialog and populate it with the
right values.

The last 2 lines check if the user actually hit the Open or OK
button and then tell Excel to open the file.

There are other direct commands so you preobably can saveAs
directly if you search the docs.

> 2) What is the most straightforward way to adjust the column widths of
> the existing excel sheets?

Again its possible by using COM to drive the workbook objects.
But again its not the best way to address this.

> Any hints/guidance/answers are greatly appreciated.

OK, Use another approach!

Use xlrd to read the data out and insert it into an HTML page.
Use the one output by Excel as a template (although you can probably 
delete most of it as unnecessary cruft produced by Microsoft.(

That gives you control over the look independent of anything
the users of Excel may do.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list