MS Word r/w in python, Python-docx issue and win32com references?

When using win32com, bear in mind that you are talking to the Word object model. You don’t need to know a lot of VBA or other languages to apply the samples to using Python; you just need to figure out which parts of the object model are being used.

Let’s take the following sample (in VBA) which will create a new instance of the Application, and load a new document into that new instance:

Public Sub NewWordApp()

    'Create variables to reference objects
    '(This line is not needed in Python; you don't need to declare variables 
    'or their types before using them)
    Dim wordApp As Word.Application, wordDoc As Word.Document

    'Create a new instance of a Word Application object
    '(Another difference - in VBA you use Set for objects and simple assignment for 
    'primitive values. In Python, you use simple assignment for objects as well.)
    Set wordApp = New Word.Application

    'Show the application
    wordApp.Visible = True

    'Create a new document in the application
    Set wordDoc = wordApp.Documents.Add()

    'Set the text of the first paragraph
    '(A Paragraph object doesn't have a Text property. Instead, it has a Range property
    'which refers to a Range object, which does have a Text property.)
    wordDoc.Paragraphs(1).Range.Text = "Hello, World!"

End Sub

A similar snippet of code in Python might look like this:

import win32com.client

#Create an instance of Word.Application
wordApp = win32com.client.Dispatch('Word.Application')

#Show the application
wordApp.Visible = True

#Create a new document in the application
wordDoc = wordApp.Documents.Add()

#Set the text of the first paragraph
wordDoc.Paragraphs(1).Range.Text = "Hello, World!"

Some links to the Word object model:

Some Python examples:

Leave a Comment