Compare/Diff two word files and create a result PDF

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()

Get all file property informations on 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])

Events in Microsoft Word and Excel

I think this must be an example from Marc Hammond. It shows how to connect to events in Microsoft Word and Excel:

# OfficeEvents - test/demonstrate events with Word and Excel.
from win32com.client import DispatchWithEvents, Dispatch
import msvcrt, pythoncom
import time, sys
import types

import threading
stopEvent = threading.Event()

def TestExcel():
    class ExcelEvents:
        def OnNewWorkbook(self, wb):
            if type(wb) != types.InstanceType:
                raise RuntimeError, "The transformer doesnt appear to have translated this for us!"
            self.seen_events["OnNewWorkbook"] = None
        def OnWindowActivate(self, wb, wn):
            if type(wb) != types.InstanceType or type(wn) != types.InstanceType:
                raise RuntimeError, "The transformer doesnt appear to have translated this for us!"
            self.seen_events["OnWindowActivate"] = None
        def OnWindowDeactivate(self, wb, wn):
            self.seen_events["OnWindowDeactivate"] = None
        def OnSheetDeactivate(self, sh):
            self.seen_events["OnSheetDeactivate"] = None
        def OnSheetBeforeDoubleClick(self, Sh, Target, Cancel):
            if Target.Column % 2 == 0:
                print "You can double-click there..."
            else:
                print "You can not double-click there..."
            # This function is a void, so the result ends up in
            # the only ByRef - Cancel.
                return 1

    class WorkbookEvents:
        def OnActivate(self):
            print "workbook OnActivate"
        def OnBeforeRightClick(self, Target, Cancel):
            print "It's a Worksheet Event"

    e = DispatchWithEvents("Excel.Application", ExcelEvents)
    e.seen_events = {}
    e.Visible=1
    book = e.Workbooks.Add()
    book = DispatchWithEvents(book, WorkbookEvents)
    print "Have book", book
#    sheet = e.Worksheets(1)
#    sheet = DispatchWithEvents(sheet, WorksheetEvents)

    print "Double-click in a few of the Excel cells..."
    print "Press any key when finished with Excel, or wait 10 seconds..."
    if not _WaitForFinish(e, 10):
        e.Quit()
    if not _CheckSeenEvents(e, ["OnNewWorkbook", "OnWindowActivate"]):
        sys.exit(1)

def TestWord():
    class WordEvents:
        def OnDocumentChange(self):
            self.seen_events["OnDocumentChange"] = None
        def OnWindowActivate(self, doc, wn):
            self.seen_events["OnWindowActivate"] = None
        def OnQuit(self):
            self.seen_events["OnQuit"] = None
            stopEvent.set()

    w = DispatchWithEvents("Word.Application", WordEvents)
    w.seen_events = {}
    w.Visible = 1
    w.Documents.Add()
    print "Press any key when finished with Word, or wait 10 seconds..."
    if not _WaitForFinish(w, 10):
        w.Quit()
    if not _CheckSeenEvents(w, ["OnDocumentChange", "OnWindowActivate"]):
        sys.exit(1)

def _WaitForFinish(ob, timeout):
    end = time.time() + timeout
    while 1:
        if msvcrt.kbhit():
            msvcrt.getch()
            break
        pythoncom.PumpWaitingMessages()
        stopEvent.wait(.2)
        if stopEvent.isSet():
            stopEvent.clear()
            break
        try:
            if not ob.Visible:
                # Gone invisible - we need to pretend we timed
                # out, so the app is quit.
                return 0
        except pythoncom.com_error:
            # Excel is busy (eg, editing the cell) - ignore
            pass
        if time.time() > end:
            return 0
    return 1

def _CheckSeenEvents(o, events):
    rc = 1
    for e in events:
        if not o.seen_events.has_key(e):
            print "ERROR: Expected event did not trigger", e
            rc = 0
    return rc

def test():
    import sys
    if "noword" not in sys.argv[1:]:
        TestWord()
    if "noexcel" not in sys.argv[1:]:
        TestExcel()
    print "Word and Excel event tests passed."

if __name__=='__main__':
    test()

Set archive bit on files in a directory

import win32file
import win32con
import os

def togglefileattribute(filename, fileattribute, value):
    """Turn a specific file attribute on or off, leaving the other
    attributes intact.
    """
    bitvector = win32file.GetFileAttributes(filename)
    if value:
        bitvector |= fileattribute
    else:
        bitvector &= ~fileattribute
    win32file.SetFileAttributes(filename, bitvector)

# Sample usage:
for file in os .listdir(r'C:\Temp\doc'):
    file = os.path.join(r'C:\Temp\doc\%s'%file)
    togglefileattribute(file, win32con.FILE_ATTRIBUTE_ARCHIVE, False)

Create Outlook tasks from Taskcoach task file

This is a script which creates Outlook tasks from each task out of a Taskcoach XML-file. I saved my task to file named “My_Tasks.tsk”. Please modify it for your needs.

from xml.dom import minidom
import win32com.client
from datetime import datetime
try:
   oOutlookApp = win32com.client.gencache.EnsureDispatch("Outlook.Application")
except:
   print "MSOutlook: unable to load Outlook"

# this should use more try/except blocks or nested blocks
#onMAPI = oOutlookApp.GetNamespace("MAPI")
#ofTasks = onMAPI.GetDefaultFolder(win32com.client.constants.olFolderTasks)


xml = r'My_Tasks.tsk'

xmldoc = minidom.parse(xml)
tcTasks = xmldoc.getElementsByTagName('task')
print 'Creating %s tasks'%len(tcTasks)

for tcTask in tcTasks:
    print 'Saving %s [%s]' % (tcTask.getAttribute('subject'), tcTask.getAttribute('status'))
    newtask = oOutlookApp.CreateItem(win32com.client.constants.olTaskItem)
    if tcTask.getAttribute('expandedContexts') != 'Dummy': #Todo: Unsichere Ecke
        newtask.Subject = tcTask.getAttribute('subject')
        for note in tcTask.getElementsByTagName('note'):
            newnote = oOutlookApp.CreateItem(win32com.client.constants.olNoteItem)
            newnote.Body += note.getAttribute('subject') + ':\n'
            for nodetext in note.getElementsByTagName('description'):
                newnote.Body += nodetext.firstChild.data
            newnote.Save()
            #newtask.Links.Add(newnote)

        for tasktext in tcTask.getElementsByTagName('description'):
                newtask.Body += tasktext.firstChild.data
        for attachment in tcTask.getElementsByTagName('attachment'):
            newtask.Body += 'File attachment %s:\n%s\n' % (attachment.getAttribute('subject'), attachment.getAttribute('location'))

        if tcTask.getAttribute('duedate') not in ['', None]:
            newtask.DueDate = tcTask.getAttribute('duedate')
        if tcTask.getAttribute('startdate') not in ['', None]:
            newtask.StartDate = tcTask.getAttribute('startdate')
        #elif tcTask.getAttribute('percentageComplete') in ['0', 0]:
            #newtask.Status = 0
            #newtask.PercentComplete = 0
        if tcTask.getAttribute('percentageComplete') in ['100', 100]:
            newtask.Status = 2
            newtask.PercentComplete = 100
        if tcTask.getAttribute('percentageComplete') not in ['', None, '100', 100]:
            newtask.PercentComplete = int(tcTask.getAttribute('percentageComplete'))
            newtask.Status = 5
        newtask.Save()

Get all adressbook entries

import codecs, win32com.client

# This example dumps the items in the default address book

# needed for converting Unicode->Ansi (in local system codepage)
DecodeUnicodeString = lambda x: codecs.latin_1_encode(x)[0]

def DumpDefaultAddressBook():
    # Create instance of Outlook
    o = win32com.client.Dispatch("Outlook.Application")
    mapi = o.GetNamespace("MAPI")
    folder = mapi.GetDefaultFolder(win32com.client.constants.olFolderContacts)
    print "The default address book contains",folder.Items.Count,"items"

    # see Outlook object model for more available properties on ContactItem objects
    attributes = [ 'FullName', 'Email1Address']    
    for i in range(1,folder.Items.Count+1):
        print "~~~ Entry %d ~~~" % i
        item = folder.Items[i]
        for attribute in attributes:
            print attribute, eval('item.%s' % attribute)
        
    o = None

DumpDefaultAddressBook()  

Events in Microsoft Project

I found this very interesting Python script, which showes how to use Events in Microsoft Project with Python for Windows. Simply dispatch with an eventmanager class:

import win32com.client
import pythoncom

defaultNamedNotOptArg = pythoncom.Empty

class EventManager(object):
    def OnProjectBeforeSave(self, pj=defaultNamedNotOptArg, SaveAsUi=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Project saved'
        pass
    def OnProjectBeforeClose(self, pj=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Project closed'
        pass
    def OnProjectTaskNew(self, pj=defaultNamedNotOptArg, ID=defaultNamedNotOptArg):
        print 'Task created'
        pass
    def OnProjectBeforeTaskChange(self, tsk=defaultNamedNotOptArg, Field=defaultNamedNotOptArg, NewVal=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Field %s of Task %s changed to %s'%(Field, tsk, NewVal)
        pass
    def OnProjectBeforeTaskDelete(self, tsk=defaultNamedNotOptArg, Cancel=defaultNamedNotOptArg):
        print 'Task %s deleted'%tsk
        pass

app = win32com.client.DispatchWithEvents('MSProject.Application', EventManager)
doc = app.ActiveProject
app.Visible=1

Remote monitoring 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 

Form extractor from word to Excel

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:

"""
Copyright 2009 Konrads Smelkovs 
UTF8Recorder and UnicodeWriter come from python docs
"""

import sys,os,csv
import win32com.client
import pywintypes


class ExcelWriter(object):
    def __init__(self,excelfile):
        self.excelapp=win32com.client.DispatchEx('Excel.Application')
        self.excelapp.Visible=0
        self.excelapp.Application.AskToUpdateLinks=0
        self.workbook=self.excelapp.Workbooks.Add()
        os.unlink(excelfile) #TODO: remove for release
        self.workbook.SaveAs(excelfile)
        # Only worksheet 1 is used.
        self.worksheet=self.workbook.Worksheets.Item(1)
        self.currentrow=1

    def _getrow(self,row):
        """Convert integer row index to Alphabetical:
        1 -> A
        2 -> B
        ...
        """
        if row<27:
            return chr(ord('A')-1 + row)
        else:
            first=row / 26
            return chr(ord('A')-1 + first) +  chr(ord('A')-1 + row % 26)
        
    def __del__(self):
        self.workbook.Save()
        self.workbook.Close()
        self.excelapp.Quit()

    def writerow(self,data):
        for col in xrange(1,len(data)+1):
            range=self._getrow(col)+str(self.currentrow)
            print >>sys.stderr,"Range: %s"  % range
            cell=self.worksheet.Range(range)
            cell.Value=data[col-1]
        self.currentrow+=1
        
def main():
 if len(sys.argv)<3:
    print "Usage: %s  " % sys.argv[0]
    print "Where  - directory containing word docs with forms"
    print "and  - file where to put results"
    sys.exit(-1)
 directory=os.path.abspath(sys.argv[1])
 wordapp = win32com.client.Dispatch("Word.Application")
 wordapp.Visible=0 # Hide word app
 results=[]
 for docfile in os.listdir(directory):
     thisdocresults=[]
     if docfile.endswith(".doc") or docfile.endswith(".docx"):
         print >> sys.stderr, "Processing %s" % docfile
         worddoc=wordapp.Documents.Open(os.path.join(directory,docfile))
         for i in range(1,worddoc.FormFields.Count+1):
            try:
                form=worddoc.FormFields.Item(i)
                name=form.Name
                value=form.Result
                thisdocresults.append((name,value))
                try:
                    print >>sys.stderr, "%s: %s" % (name,value)
                except UnicodeEncodeError,e:
                    print >>sys.stderr, "Error decoding charset,%s" % e
            except pywintypes.com_error,e:
                print >>sys.stderr, "Exception: %s" % str(e)
         results.append(thisdocresults)
         worddoc.Close()
 wordapp.Quit()
 writer=ExcelWriter(os.path.abspath(sys.argv[2]))
 print >>sys.stderr,"Writing to Excel"
 for docres in results:
     data=[]
     for (n,v) in docres:
         data.append(v)
     writer.writerow(data)
 
if __name__=="__main__":
    main()

Change Desktop color with ctypes

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)))