Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Opening MS Word files via Python

Reply
Thread Tools

Opening MS Word files via Python

 
 
Fazer
Guest
Posts: n/a
 
      04-21-2004
Here comes another small question from me

I am curious as to how I should approach this issue. I would just
want to parse simple text and maybe perhaps tables in the future.
Would I have to save the word file and open it in a text editor? That
would kind of....suck... Has anyone else tackled this issue?

Thanks,
 
Reply With Quote
 
 
 
 
Rob Nikander
Guest
Posts: n/a
 
      04-21-2004
Fazer wrote:
> I am curious as to how I should approach this issue. I would just
> want to parse simple text and maybe perhaps tables in the future.
> Would I have to save the word file and open it in a text editor? That
> would kind of....suck... Has anyone else tackled this issue?


The win32 extensions for python allow you to get at the COM objects for
applications like Word, and that would let you get the text and tables.
google: win32 python.

word = win32com.client.Dispatch('Word.Application')
word.Documents.Open('C:\\myfile.doc')

But I don't know the best way to find out the methods and properties of
the "word" object.

Rob

 
Reply With Quote
 
 
 
 
Simon Brunning
Guest
Posts: n/a
 
      04-21-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Fazer) wrote in message
> I am curious as to how I should approach this issue. I would just
> want to parse simple text and maybe perhaps tables in the future.
> Would I have to save the word file and open it in a text editor? That
> would kind of....suck... Has anyone else tackled this issue?


See http://aspn.activestate.com/ASPN/Coo.../Recipe/279003

Cheers,
Simon B.
 
Reply With Quote
 
jmdeschamps
Guest
Posts: n/a
 
      04-21-2004
Rob Nikander <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Fazer wrote:
> > I am curious as to how I should approach this issue. I would just
> > want to parse simple text and maybe perhaps tables in the future.
> > Would I have to save the word file and open it in a text editor? That
> > would kind of....suck... Has anyone else tackled this issue?

>
> The win32 extensions for python allow you to get at the COM objects for
> applications like Word, and that would let you get the text and tables.
> google: win32 python.
>
> word = win32com.client.Dispatch('Word.Application')
> word.Documents.Open('C:\\myfile.doc')
>
> But I don't know the best way to find out the methods and properties of
> the "word" object.
>
> Rob


You can use VBA documentation for Word, and using dot notation and
normal Pythonesque way of calling functions, play with its diverses
objects, methods and attributes...
Here's some pretty straightforward code along these lines:
#************************
import win32com.client
import tkFileDialog

# Launch Word
MSWord = win32com.client.Dispatch("Word.Application")
MSWord.Visible = 0
# Open a specific file
myWordDoc = tkFileDialog.askopenfilename()
MSWord.Documents.Open(myWordDoc)
#Get the textual content
docText = MSWord.Documents[0].Content
# Get a list of tables
listTables= MSWord.Documents[0].Tables
#************************

Happy parsing,

Jean-Marc
 
Reply With Quote
 
Fazer
Guest
Posts: n/a
 
      04-24-2004
(E-Mail Removed) (jmdeschamps) wrote in message news:<(E-Mail Removed). com>...
> Rob Nikander <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> > Fazer wrote:
> > > I am curious as to how I should approach this issue. I would just
> > > want to parse simple text and maybe perhaps tables in the future.
> > > Would I have to save the word file and open it in a text editor? That
> > > would kind of....suck... Has anyone else tackled this issue?

> >
> > The win32 extensions for python allow you to get at the COM objects for
> > applications like Word, and that would let you get the text and tables.
> > google: win32 python.
> >
> > word = win32com.client.Dispatch('Word.Application')
> > word.Documents.Open('C:\\myfile.doc')
> >
> > But I don't know the best way to find out the methods and properties of
> > the "word" object.
> >
> > Rob

>
> You can use VBA documentation for Word, and using dot notation and
> normal Pythonesque way of calling functions, play with its diverses
> objects, methods and attributes...
> Here's some pretty straightforward code along these lines:
> #************************
> import win32com.client
> import tkFileDialog
>
> # Launch Word
> MSWord = win32com.client.Dispatch("Word.Application")
> MSWord.Visible = 0
> # Open a specific file
> myWordDoc = tkFileDialog.askopenfilename()
> MSWord.Documents.Open(myWordDoc)
> #Get the textual content
> docText = MSWord.Documents[0].Content
> # Get a list of tables
> listTables= MSWord.Documents[0].Tables
> #************************
>
> Happy parsing,
>
> Jean-Marc



That is Awesome! Thanks!

How would I save something in word format? I am guessing
MSWord.Docments.Save(myWordDoc) or around those lines? where can I
find more documentatin? Thanks.
 
Reply With Quote
 
anon
Guest
Posts: n/a
 
      04-24-2004
Fazer wrote...

> (E-Mail Removed) (jmdeschamps) wrote in message news:<(E-Mail Removed). com>...
>
>>Rob Nikander <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

<snip>
>>>
>>>But I don't know the best way to find out the methods and properties of
>>>the "word" object.
>>>

<snip>
>
> How would I save something in word format? I am guessing
> MSWord.Docments.Save(myWordDoc) or around those lines? where can I
> find more documentatin? Thanks.




Open MS Word and press (ALT + F11), then F2





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deployment of a WebApp that uses Word 10.0 via Interop.Word.dll object - How do I register the COM?? Luis Esteban Valencia ASP .Net 2 09-26-2008 08:26 PM
Word file not opening from Adm -My recent docs but opening from file menu of Word Thaqalain Computer Support 0 06-30-2005 02:20 AM
opening a word doc in word not browser Stephen Witter ASP .Net 0 05-18-2004 02:48 AM
RE: Opening MS Word files via Python Tim Golden Python 0 04-21-2004 08:33 AM
Reading WORD files without opening? Serendipity Computer Support 2 10-19-2003 06:07 PM



Advertisments