Skip to content

Instantly share code, notes, and snippets.

@ninthhostage
Last active April 14, 2024 07:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ninthhostage/0ddf331c7585c70e69e68b896f41e83f to your computer and use it in GitHub Desktop.
Save ninthhostage/0ddf331c7585c70e69e68b896f41e83f to your computer and use it in GitHub Desktop.
Updating MS Word Fields with Python

Update Fields

Updating fields requires using the Word application itself via COM (Component Object Model) objects.

You're looking for the Fields.Update method See: https://docs.microsoft.com/en-us/office/vba/api/word.fields.update.

The below should be the python equivalent of CTRL+A F9 from withing the Word GUI.

Code Snippet:

import sys
import os
import comtypes.client

filepath = '' # put file path here

word = comtypes.client.CreateObject('Word.Application') # opens the word application
doc = word.Documents.Open(filepath) # opens the specified file
doc.Fields.Update() # updates field, returns 0 if successful
doc.Save()
word.Quit()

Sample Function:

import sys
import os
import comtypes.client

def update_fields(filepath):
    folder, filename = os.path.split(filepath)
    extension = os.path.splitext(filename)[1]
    acceptable_ext = ['.doc','.docx']
    word = comtypes.client.CreateObject('Word.Application') # opens the word application

    if extension not in acceptable_ext:
        print('Error: Incorrect Filetype ' + extension)
    else:    
        doc = word.Documents.Open(filepath) # opens the specified file
        response = doc.Fields.Update() # updates field, returns 0 if successful
        if response != 0:
            print('Error with ' + filename + ' on field at Index '+str(response))
        if response == 0:
            doc.Save(NoPrompt=True, OriginalFormat=1)
            print('Success with ' + filename)
        doc.Close()
    word.Quit()


filepath = '' # put path here
update_fields(filepath)

Sample Function with many documents:

import sys
import os
import comtypes.client

def update_fields(filepath):
    folder, filename = os.path.split(filepath)
    extension = os.path.splitext(filename)[1]
    acceptable_ext = ['.doc','.docx']
    word = comtypes.client.CreateObject('Word.Application') # opens the word application

    if extension not in acceptable_ext:
        print('Error: Incorrect Filetype ' + extension)
    else:    
        doc = word.Documents.Open(filepath) # opens the specified file
        response = doc.Fields.Update() # updates field, returns 0 if successful
        if response != 0:
            print('Error with ' + filename + ' on field at Index '+str(response))
        if response == 0:
            doc.Save(NoPrompt=True, OriginalFormat=1)
            print('Success with ' + filename)
        doc.Close()
    word.Quit()

filepaths = [] # put list of paths here

for filepath in filepaths:
    update_fields(filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment