[Tutor] Win32Com.client help

Gardner, Dean Dean.Gardner at barco.com
Thu Jul 20 10:13:46 CEST 2006


Apologies 

Here is the accessor script 


import win32com.client
import tkFileDialog
import string, sys
 

from Excel import *

import word

#Example of Python controlling external apps

#number of the worksheet to be examined
_workSheetNumber = 1

choice = int(raw_input("Do you want to open:\n1) An Excel File\n2) A Word
File\n3) Exit\nPlease Select....."))
if choice==1:
         
    # Open a specific file
    myExcelDoc = tkFileDialog.askopenfilename()
    #open the excel document
    xlOpen = Excel(myExcelDoc)
    print xlOpen

    #obtain a value from a particular cell
    cellValue = xlOpen.get_cell(_workSheetNumber,3,10)
    print cellValue

    #obtain a 2d tuple from a range of cells
    range_of_cells = xlOpen.get_range(_workSheetNumber,1,1,20,3)

    #print the result form getRange()
    for item in range_of_cells:
        for secondItem in range_of_cells:
            print "Example: " +str(item)+":"+str(secondItem)

    #close the file
    xlOpen.close()

elif choice==2:
    # Open a specific word file
    myWordDoc = tkFileDialog.askopenfilename()
    print myWordDoc
    #create new word object
    wdDoc = word.word(myWordDoc)
    #get the file contents
    try:
        docText =unicode("","utf-8")
        tempDoc = wdDoc.getFileContents()
        docText += tempDoc
        print docText
        wdDoc.close()
    except UnicodeEncodeError:
        print "This file cannot be opened due to a unicode error"
        wdDoc.close()
            
elif choice==3:
    sys.exit(0)


With a sample word file looking something like this

Monday
Tuesday
Wednesday
Thursday
Friday

Only friday is printed out. 

With a longer file (I cannot post the exact contents) like

00000 Description
00001 Description1
00002 Description2
00003 Description3
00004 Description4
00005 Description5

Then 00000,00001,00004 is printed out

Dean Gardner
Test Engineer 
Barco
Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799
www.barco.com 
dean.gardner at barco.com 
Unless indicated otherwise, the information contained in this message is
privileged and confidential, and is intended only for the use of the
addressee(s) named above and others who have been specifically authorized to
receive it. If you are not the intended recipient, you are hereby notified
that any dissemination, distribution or copying of this message and/or
attachments is strictly prohibited. The company accepts no liability for any
damage caused by any virus transmitted by this email. Furthermore, the
company does not warrant a proper and complete transmission of this
information, nor does it accept liability for any delays. If you have
received this message in error, please contact the sender and delete the
message. Thank you. 




-----Original Message-----
From: Bob Gailer [mailto:bgailer at alum.rpi.edu]
Sent: 18 July 2006 18:45
To: Gardner, Dean
Cc: tutor at python.org
Subject: Re: [Tutor] Win32Com.client help


Gardner, Dean wrote:
>
> Hi
>
> I have been looking at simple examples of controlling applications 
> like excel and word using python. All seems well until I read in a 
> word document with multiple lines. It then seems to pick out seemingly 
> random sections of the document to display.
>
> This is the class that is handling the opening etc of the document
>
Please show us the relevant parts of the program. Most of the methods 
you posted have nothing to do with the problem, and the crucial code for 
instantiating the class and calling its methods is missing, as is an 
example of the desired output vs the actual output.
No can help without this.
>
> class word:
>     def __init__(self, filename=None):
>         """
>            @ Opens a word document specified by the user
>         """
>         print "Initiating and opening..."+str(filename)
>        
>         self.wdApp = win32com.client.Dispatch('Word.Application')     
>         self.wdDoc = self.wdApp.Documents.Open(filename)
>        
>     def save(self, newfilename=None):
>         """ Saves the open document with
>             a user defined new filename
>         """
>         if newfilename:
>             self.filename = newfilename
>             self.wdDoc.SaveAs(newfilename)
>         else:
>             self.wdDoc.Save()
>    
>       
>     def close(self):
>        
>         print "Preparing to close"
>         #self.wdDoc.Close()
>         self.wdDoc.Close()
>         self.wdApp.Quit()
>        
>         #del self.wdApp
>
>     def show(self):
>         self.wdApp.Visible = 1
>
>     def hide(self):
>         self.wdApp.Visible = 0
>        
>    
>     def getFileContents(self):
>         print "Get file contents"
>         docString = self.wdApp.Documents[0].Content
>        
>         return str(docString).decode('latin-1')
>
> *Dean Gardner
> *Test Engineer
> Barco
> Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
> Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799
> _www.barco.com _
> _dean.gardner at barco.com _
> Unless indicated otherwise, the information contained in this message 
> is privileged and confidential, and is intended only for the use of 
> the addressee(s) named above and others who have been specifically 
> authorized to receive it. If you are not the intended recipient, you 
> are hereby notified that any dissemination, distribution or copying of 
> this message and/or attachments is strictly prohibited. The company 
> accepts no liability for any damage caused by any virus transmitted by 
> this email. Furthermore, the company does not warrant a proper and 
> complete transmission of this information, nor does it accept 
> liability for any delays. If you have received this message in error, 
> please contact the sender and delete the message. Thank you.
>
>
> - - - - - - - DISCLAIMER- - - - - - - -
>
> Unless indicated otherwise, the information contained in this message 
> is privileged and confidential, and is intended only for the use of 
> the addressee(s) named above and others who have been specifically 
> authorized to receive it. If you are not the intended recipient, you 
> are hereby notified that any dissemination, distribution or copying of 
> this message and/or attachments is strictly prohibited. The company 
> accepts no liability for any damage caused by any virus transmitted by 
> this email. Furthermore, the company does not warrant a proper and 
> complete transmission of this information, nor does it accept 
> liability for any delays. If you have received this message in error, 
> please contact the sender and delete the message. Thank you.
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   


-- 
Bob Gailer
510-978-4454

- - - - - - - DISCLAIMER- - - - - - - -
Unless indicated otherwise, the information contained in this message is
privileged and confidential, and is intended only for the use of the
addressee(s) named above and others who have been specifically authorized to
receive it. If you are not the intended recipient, you are hereby notified
that any dissemination, distribution or copying of this message and/or
attachments is strictly prohibited. The company accepts no liability for any
damage caused by any virus transmitted by this email. Furthermore, the
company does not warrant a proper and complete transmission of this
information, nor does it accept liability for any delays. If you have
received this message in error, please contact the sender and delete the
message. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20060720/e95048a7/attachment.htm 


More information about the Tutor mailing list