New to COM (Excel). Need a little help.
Peter Abel
PeterAbel at gmx.net
Fri Oct 3 18:01:59 EDT 2003
mnations at airmail.net (Marc) wrote in message news:<4378fa6f.0310030818.65086abe at posting.google.com>...
> Hi all,
>
> I am trying to write an application where I need the ability to open
> an Excel spreadsheet and do basic read/write, insert rows, and
> hide/unhide rows. Using win32com I have been able to get the basics
> down as well as some examples displaying how to simply read and write.
>
> But the next step appears exponential. I haven never done anything in
> VB, so any and all concepts and commands are completely foreign. I
> have been digging through the VB help and also bought a book
> specifically for Python and COM. But I don't really have time to learn
> VB before I can finish my script.
>
> Would there happen to be any pre-existing examples of the stuff I need
> to do out there? Basically I need to do the things I listed above -
> insert rows and columns and hide/unhide rows. I think with a few
> examples of sheet manipulation I could figure out the rest.
>
> Thanks ahead of time,
> Marc
One trick is using Excel's Macro-Recorder to record all
you've done in Excel. After this you get the resulting macro
by Alt-F11 in the VBA-Editor.
You can copy the recorded methode-calls to Python by changing some
few things to Python-syntax.
Let's say you have reached the following point:
>>> import win32com.client
>>> excel = win32com.client.Dispatch("Excel.Application")
>>> excel.Visible=1
>>> workbook=excel.Workbooks.Add()
Now start Excel's Macro-Recorder and do want you want to do.
E.g. select row 4-9 and hide them.
Stop recording and see the VBA-result:
Rows("4:9").Select
Selection.EntireRow.Hidden = True
This will result in the following Python code:
>>> excel.Rows("4:9").Select()
>>> excel.Selection.EntireRow.Hidden = True
Et voila, line 4 to 9 are hidden
(provided that you got them unhided before in Excel).
Some days or weeks later when you have learned a little bit
about ??Microsoft's VBA-OOP-concept?? :-) you will know that you
can code this shorter:
>>> workbook.ActiveSheet.Rows("4:9").Hidden = True
or quicker:
>>> excel.Rows("4:9").Hidden = True
I hope you will likes this **LEARNING by RECORDING** :-)
Regards
Peter
More information about the Python-list
mailing list