[Tkinter-discuss] How to open a Microsoft Word file from Tkinter?

Peter Milliken peter.milliken at gmail.com
Sun Jul 19 22:11:52 CEST 2009


I have some code from several years ago where I access Word documents that
are configured to perform mail merges i.e. I add the data from a python
program and then print the .doc file. Unfortunately I didn't document it
very well (presumably I understood it at the time? It certainly worked!
:-)).
So here are some clues from that code (it was part of a lane management
system I wrote for my kids swimming club):

from win32com.client import constants, Dispatch
from win32com.gen_py.word import *

    wd = Dispatch("Word.Application")

    for l in range(1, int(self['lanes']) + 1):
      # The Word documents will be in the same directory as the code.
      lane_name = '%s\Lane%s.doc' % (os.getcwd(), l)

      doc = wd.Documents.Open(lane_name)

      event_range = range(int(self.component('StartEvent').getvalue()),
                          self.num_events,
                          3)            # Number of events per page

      doc.MailMerge.Destination =
win32com.gen_py.word.constants.wdSendToNewDocument
      doc.MailMerge.MailAsAttachment = False
      doc.MailMerge.MailAddressFieldName = ""
      doc.MailMerge.MailSubject = ""
      doc.MailMerge.SuppressBlankLines = True

      for event in event_range:
        doc.MailMerge.DataSource.FirstRecord = event
        doc.MailMerge.DataSource.LastRecord = event + 2
        doc.MailMerge.Execute(Pause = True)
        wd.ActiveDocument.PrintOut()
        wd.ActiveDocument.Close(SaveChanges = False)

      # Now close the mailmerge template
      doc.Close(SaveChanges = False)

I most likely copied this code from somewhere on the Internet - I would love
to give credit, but have long since lost the information...

Hope this helps,
Peter


On Sat, Jul 18, 2009 at 12:13 AM, Guilherme Polo <ggpolo at gmail.com> wrote:

> On Fri, Jul 17, 2009 at 9:15 AM, The Helmbolds<helmrp at yahoo.com> wrote:
> > I managed to open a plain *.txt file in Microsoft Notepad from TkInter by
> > using Python's "spawnv" command in its "os" module.
> >
> > That does not seem to work for opening a *.txt or a *.doc file in
> Microsoft
> > Word.
> >
> > How do I open a *.doc file in Microsoft Word from TkInter? Or is this so
> > complicated it's not worth it?
> >
>
> What happens if you try os.system('start myfile.doc') ?
>
> This is not related to Tkinter, so, if you want to extend the initial
> question and include communication with ms word or anything related, I
> would suggest to post this to an appropriate list.
>
> Regards,
>
>
>
> --
> -- Guilherme H. Polo Goncalves
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20090720/41ce34fe/attachment.htm>


More information about the Tkinter-discuss mailing list