Convert Word DOCX to DOC or DOC to DOCX with Python

Alice Yang
3 min readSep 26, 2023

--

Convert Word Docx to Doc or Doc to Docx with Python
Convert Word Docx to Doc or Doc to Docx with Python

Understand the Difference between DOCX and DOC Formats

The DOCX format was introduced with Microsoft Word 2007 and is based on open XML (Extensible Markup Language). It offers enhanced capabilities, improved file compression, and better data recovery options. On the other hand, the DOC format is the older binary format used by earlier versions of Word, such as Word 97–2003. While the DOCX format is more widely supported and recommended, there may be instances where you need to convert between the two formats to accommodate specific requirements or compatibility issues.

Python Library to Convert Between Word DOCX and DOC Formats

To convert between Word DOCX and DOC formats with Python, we can use the Spire.Doc for Python library.

Spire.Doc for Python is a feature-rich and easy-to-use library for creating, reading, editing, and converting Word files within Python applications. With this library, you can work with a wide range of Word formats, including Doc, Docx, Docm, Dot, Dotx, Dotm, and more. Moreover, you are also able to render Word documents to other types of file formats, such as PDF, RTF, HTML, Text, Image, SVG, ODT, PostScript, PCL, and XPS.

You can install Spire.Doc for Python from PyPI by running the following command in your terminal:

pip install Spire.Doc

For more detailed information about the installation, you can check this official documentation: How to Install Spire.Doc for Python in VS Code.

Convert Word DOC to DOCX with Python

Spire.Doc for Python provides the Document.SaveToFile(str, FileFormat) method, which enables seamless conversion between different Word formats. By specifying the appropriate FileFormat parameter, such as FileFormat.Docx, FileFormat.Docx2013 or FileFormat.Docx2016, you can easily save DOC files to DOCX file format.

Here is a simple example that shows how to convert a DOC file to DOCX format using Spire.Doc for Python:

from spire.doc import *
from spire.doc.common import *

# Create an object of the Document class
document = Document()
# Load a Word DOC file
document.LoadFromFile("Sample.doc")

# Save the DOC file to DOCX format
document.SaveToFile("ToDocx.docx", FileFormat.Docx2016)
# Close the Document object
document.Close()

Convert Word DOCX to DOC with Python

By setting the FileFormat parameter in the Document.SaveToFile(str, FileFormat) method to FileFormat.Doc, you can easily save a DOCX file as DOC format.

Here is a simple example that shows how to convert a DOCX file to DOC format using Spire.Doc for Python:

from spire.doc import *
from spire.doc.common import *

# Create an object of the Document class
document = Document()
# Load a Word DOCX file
document.LoadFromFile("Sample.docx")

# Save the DOCX file to DOC format
document.SaveToFile("ToDoc.doc", FileFormat.Doc)
# Close the Document object
document.Close()

Conclusion

In this article, we have outlined how to perform conversions between DOCX and DOC formats using Python and Spire.Doc for Python library. In addition to this, you can utilize Spire.Doc for Python to implement conversions from DOCX or DOC to other Word formats, such as DOCM, DOT, DOTM, and DOTX. Furthermore, you are also able to convert Word documents to other file formats, such as PDF, Images, Text, HTML, OpenXml, ODT, PCL, and XPS.

--

--

Alice Yang

Skilled senior software developers with five years of experience in all phases of software development life cycle using .NET, Java and C++ languages.