Download pptx - win32com Tricks and Secrets

Transcript
Page 1: win32com Tricks and Secrets

win32com Tricks and SecretsThe essential tools for using com objects in Python

Presented by Craig [email protected] Division of Water Resources

November 9, 2010

Page 2: win32com Tricks and Secrets

What Tricks (you might ask)Create reports with WordCreate tables, charts, graphicsUse any database accessible

through ADODB.Translate existing VB, C+ into

python to use the same com objects

Etc.

Page 3: win32com Tricks and Secrets

Download/Install Pythonwin Navigate to http://sourceforge.net/projects/pywin32/

Select this

To view this

Computer/System

Python version

Page 4: win32com Tricks and Secrets

Where is PythonWin installed?ArcGIS 10 setup

Page 5: win32com Tricks and Secrets

Basic example (will work after install)Import win32com.client xlApp = win32com.client.Dispatch(

"Excel.Application")xlApp.Visible = TruexlApp.WorkBooks.Add()

Note: Should show Excel with 3 standard worksheets, sheet1, sheet2, sheet3

Page 6: win32com Tricks and Secrets

What Strings to Use with DispatchDo not use Task Manager to find

strings. Old help suggests this but now doesn’t work.

For Word use “Word.Application”For other applications use

MakePy

Page 7: win32com Tricks and Secrets

Create them with MakePy

Page 8: win32com Tricks and Secrets

Select your Com objects and press

“OK”results below

Page 9: win32com Tricks and Secrets

Look at Code in Script

Page 10: win32com Tricks and Secrets

Where to get helpCreate macros and translate the

code from Word or Excel.Use .chm helpfiles from

Microsoft, for example vbalx10.chm for excel.

Page 11: win32com Tricks and Secrets

Use ADO210.CHM for ADODB

Page 12: win32com Tricks and Secrets

Use JETSQL40.chm for SQL

Page 13: win32com Tricks and Secrets

More .chm files are available

Page 14: win32com Tricks and Secrets

What you get after DispatchNote: This only works in PythonWin

You can now inspect xlApp’s methods

Page 15: win32com Tricks and Secrets

Rules are for following!If you use MakePy

◦…. and distribute your code, distribute the files created by MakePy.

Suggestions for modifying VBA to work in python:Case for methods and attributes matters.

RS.MoveFirst() not RS.movefirst()Always use () with methods

RS.MoveFirst() not RS.MoveFirstAttributes or values don’t need ()

◦while not RS.EOF: acceptableDon’t use shortcuts! (Use PythonWin here)

RS.Fields(“ID”).Value not RS(“ID”)

Page 16: win32com Tricks and Secrets

If you use MakePyMake sure that you also include

the proper COM files if you use a specific version.

Consider using generic versions by simply deleting the version number, ie. “ADODB.Connection” not “ADODB.Connection.6.0”

Page 17: win32com Tricks and Secrets

HintsDevelop initial code in VB, C+, or

C++Can integrate python and ruby

into Visual Studio 2010. Less need for these workarounds when VS 2010 is standard for ArcGIS.

You can integrate python into microsoft code now if you register it!

Page 18: win32com Tricks and Secrets

Need More Help??

Page 19: win32com Tricks and Secrets

Thank You!Questions?Email: [email protected]: 801-538-7280


Recommended