Examples for automating Windows applications via the Microsoft
Component Object Model (COM), Win32API, PythonCOM and other interesting
Python modules. Mainly developed with the Python Windows Extensions from Marc Hammond. If you want to post a script, article or news please use this link Post a Python script. Hint: For sytax highlighting place your code between the tags <pre> and </pre>
This section as RSS feed.
|
|
Microsoft Windows
|
import win32clipboard
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText(text)
win32clipboard.CloseClipboard()
|
|
|
CAD Systems
|
import os, sys
import win32com.client
def convert(file, format):
print ' Converting with CATIA V5'
dirname, filename = os.path.split(file)
basename, ext = os.path.splitext(filename)
fileout=os.path.join(dirname, basename + '.' + format)
try:
CATIA = win32com.client.Dispatch('CATIA.Application')
except:
print 'Error connecting to CATIA! Either CATIA V5 is not installed or'
'you didn`t have registered CATIA V5 as a COM application.'
print 'You can do this with:'
print ' >> c:\\...\\Dassault Systemes\\intel_a\\code\\bin\\CNEXT.exe /regserver'
sys.exit()
CATIA.DisplayFileAlerts = False
Doc = CATIA.Documents.Open(os.path.abspath(file))
try:
Doc.ExportData(fileout, format)
except:
print 'Could not convert file %s.'%file
finally:
Doc.Close()
CATIA.Quit()
print 'done'
return fileout
if __name__ == "__main__":
if len(sys.argv)==1:
print 'Usage: v5batcher.py [filename] [format]'
else:
convert(sys.argv[1], sys.argv[2])
|
|
|
Microsoft Office
|
import win32ui
o = win32ui.CreateFileDialog( 1, ".txt", "default.txt", 0, "Text Files (*.txt)|*.txt|All Files (*.*)|*.*|")
o.DoModal()
print o.GetPathName()
|
|
|
Microsoft Office
|
import win32com.client
import time
app = win32com.client.Dispatch('Word.Application')
# The original document:
doc = app.Documents.Open('c:\\temp\\firstversion.doc')
# The modified document:
ret = doc.Compare('c:\\temp\\newversion.doc')
app.ActiveDocument.SaveAs('diff.doc')
app.ActiveDocument.ExportAsFixedFormat('diff.pdf', 17, False, 0, 0, 1, 1, 7, True, True, 1, True, True, True)
app.ActiveDocument.Close()
doc.Close()
app.Quit()
|
|
|
Microsoft Windows
|
# DumpStorage.py - Dumps some user defined properties
# of a COM Structured Storage file.
import pythoncom
from win32com import storagecon # constants related to storage functions.
# These come from ObjIdl.h
FMTID_UserDefinedProperties = "{F29F85E0-4FF9-1068-AB91-08002B27B3D9}"
PIDSI_TITLE = 0x00000002
PIDSI_SUBJECT = 0x00000003
PIDSI_AUTHOR = 0x00000004
PIDSI_CREATE_DTM = 0x0000000c
def PrintStats(filename) :
if sys.platform == 'win32':
if not pythoncom.StgIsStorageFile(filename) :
print "The file is not a storage file!"
return
# Open the file.
flags = storagecon.STGM_READ | storagecon.STGM_SHARE_EXCLUSIVE
stg_= pythoncom.StgOpenStorage(filename, None, flags )
# Now see if the storage object supports Property Information.
try:
pss = stg_.QueryInterface(pythoncom.IID_IPropertySetStorage)
except pythoncom.com_error:
print "No summary information is available"
return
# Open the user defined properties.
ps = pss.Open(FMTID_UserDefinedProperties)
props = PIDSI_TITLE, PIDSI_SUBJECT, PIDSI_AUTHOR, PIDSI_CREATE_DTM
data = ps.ReadMultiple( props )
# Unpack the result into the items.
title, subject, author, created = data
print "Title:", title
print "Subject:", subject
print "Author:", author
print "Created:", created.Format()
if __name__=='__main__':
import sys
if len(sys.argv)<2:
print "Please specify a file name"
else:
PrintStats(sys.argv[1])
|
|
|
<< Start < Prev 1 2 3 4 5 6 7 8 9 10 Next > End >>
|
| Results 1 - 5 of 95 |