Batch Convert .doc to .docx Using Python and win32com
This guide explains how to handle mixed .doc and .docx files by installing the win32com library, providing step‑by‑step code to batch convert .doc documents to .docx format and then process them with python‑docx for automated office workflows.
Problem Introduction
In daily work we often need to process large numbers of Excel and Word files. When faced with mixed .xls/.xlsx/.xlsm or .doc/.docx files, using VBA is possible but Python offers a much simpler batch solution.
This article focuses on converting .doc files to .docx so they can be processed with python-docx, which only supports the newer format.
Installing win32com
If pip install win32com fails, use the alternative command:
python -m pip install pypiwin32win32com Syntax Overview
The following snippet shows how to launch Word via COM, run it in the background, and suppress alerts:
# Call Word application
WordApp = win32com.client.Dispatch("Word.Application")
# Run in background, no UI, no alerts
WordApp.Visible = 0
WordApp.DisplayAlerts = 0Full Conversion Script (Procedural Style)
import os
import time
import win32com
from win32com.client import Dispatch
def doc_to_docx(path):
w = win32com.client.Dispatch('Word.Application')
w.Visible = 0
w.DisplayAlerts = 0
doc = w.Documents.Open(path)
# Absolute path required
newpath = allpath + '\\ConvertedDocument.docx'
time.sleep(3) # pause to avoid error -2147352567
doc.SaveAs(newpath, 12, False, "", True, "", False, False, False, False, False)
# doc.Close() # Uncomment to delete original .doc
w.Quit() # exit Word
return newpath
allpath = os.getcwd()
print(allpath)
# Example usage
doc_to_docx(allpath + '\\OriginalDocument.doc')Result
The script converts each .doc file in the specified directory to a .docx file, ready for further automated processing.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
