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
|
This script requests following informations remotely from a Windows machine:
- Uptime
- CPU Utilization
- Available Memory
- Memory Used
- Ping
Attention: This script needs the WMI module from Tim Golden.
import re
import wmi
from subprocess import Popen, PIPE
# this script comes from http://coreygoldberg.blogspot.com/2008/12/python-monitor-windows-remotely-with.html
def get_uptime(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
secs_up = int([uptime.SystemUpTime for uptime in c.Win32_PerfFormattedData_PerfOS_System()][0])
hours_up = secs_up / 3600
return hours_up
def get_cpu(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
utilizations = [cpu.LoadPercentage for cpu in c.Win32_Processor()]
utilization = int(sum(utilizations) / len(utilizations)) # avg all cores/processors
return utilization
def get_mem_mbytes(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
available_mbytes = int([mem.AvailableMBytes for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return available_mbytes
def get_mem_pct(computer, user, password):
c = wmi.WMI(computer=computer, user=user, password=password, find_classes=False)
pct_in_use = int([mem.PercentCommittedBytesInUse for mem in c.Win32_PerfFormattedData_PerfOS_Memory()][0])
return pct_in_use
def ping(host_name):
p = Popen('ping -n 1 ' + host_name, stdout=PIPE)
m = re.search('Average = (.*)ms', p.stdout.read())
if m:
return True
else:
raise Exception
|
|
|
Microsoft Office
|
|
Konrads Smelkovs had a bunch of filled out word documents with word forms in them and neded them in Excel. Initially he tried CSV but it didn't play nice with encodings. So he decided to write directly to XLS:
|
|
|
Other
|
|
This script shows (by web scraping) the number of backlinks which Google found. import datetime, urllib2, re
url='http://google.com/search?' # Set web search URL
hits={'link':'http://www.goermezer.de/'}
# Generate web search term
hits['web search term']=urllib2.quote('link:'+hits['link'])
# Execute web search
urlstr='%sq=%s'%(url,hits['web search term'])
url=urllib2.Request(urlstr)
url.add_header('User-Agent','')
url=urllib2.urlopen(url).read()
# Store date and time of web search
hits['datetimeutc']=datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
# Parse web search results to determine hits
hits['hits']=re.search('Results <b>1</b> - <b>10</b> of about <b>(?P<hits>.+?)</b>',url)
if hits['hits']!=None:
hits['hits']=hits['hits'].group('hits')
hits['hits']=hits['hits'].replace(',','')
hits['hits']=int(hits['hits'])
else:
hits['hits']=0
print hits['hits']
|
|
|
Other
|
from ctypes.wintypes import windll, c_int, byref, RGB
COLOR_BACKGROUND = 1 # from winuser.h or win32con
SetSysColors = windll.user32.SetSysColors
which_color = RGB(255,0,0) # red
SetSysColors(1, byref(c_int(COLOR_BACKGROUND)), byref(c_int(which_color)))
|
|
|
Microsoft Windows
|
|
An example how to use VBa IProgressDialog interface from Python: http://code.activestate.com/recipes/576821/
|
|
|
<< Start < Prev 1 2 3 4 5 6 7 8 9 10 Next > End >>
|
| Results 11 - 15 of 95 |