Sitemap

Effortlessly Convert Between Text and Word DOC or DOCX Formats Using Python

4 min readDec 4, 2024

--

Python Convert Word to Text or Text to Word
Python Convert Word to Text or Text to Word

Converting between plain text and Word documents is a common task in fields such as content creation, data management, and report generation. Whether you’re simplifying documents by saving them as .txt files or extracting text from Word documents, mastering this conversion process can boost productivity and streamline your workflow.

In this blog, we’ll cover:

Why Convert Between Text and Word?

Understanding when and why to convert between plain text and Word documents can help you choose the right format for your needs.

  1. Simplicity and Compatibility
    Plain text files (.txt) are universally compatible, making them ideal for cross-platform sharing. There’s no concern about formatting issues, and they can be opened on virtually any device. In contrast, Word documents offer rich formatting, images, and tables — perfect for creating polished, professional documents with advanced structure and visual elements.
  2. Editing and Formatting Flexibility
    Plain text files are lightweight, simple to edit, and well-suited for automation tasks or storing logs. However, Word documents provide extensive formatting options such as headers, fonts, colors, and styles, making them indispensable for creating professional-quality reports, presentations, and collaborative documents.
  3. Storage and Accessibility
    Text files are compact, requiring minimal storage space, which makes them ideal for simple storage and archiving. On the other hand, Word documents offer a much more structured and visually appealing layout, which is essential for formal sharing, printing, and collaborating with others.

Tool for Converting Between Text and Word in Python

To perform conversions between text and Word in Python, we’ll need a suitable Word library. This blog will use a library called Spire.Doc for Python.

About Spire.Doc
Spire.Doc for Python is a standalone library designed for programmatically managing Word documents. It supports creating, reading, and editing Word documents in various formats, such as .doc, .docx, .dot, .dotx, and .dotm, without requiring Microsoft Office or other software. Additionally, it allows conversions to formats like PDF, RTF, HTML, plain text, images, SVG, ODT, and more.

Get Alice Yang’s stories in your inbox

Join Medium for free to get updates from this writer.

Installation
To install Spire.Doc for Python, run the following command in the terminal window:

pip install Spire.Doc

For a detailed guide, refer to the documentation on How to Install Spire.Doc for Python in VS Code.

Converting Text to Word in Python

Spire.Doc allows generating Word documents from plain text or existing .txt files.

Converting Plain Text to Word

To convert plain text into a Word document:

  1. Create a new Word document using the Document class.
  2. Use the Paragraph.AppendText() method to add text.
  3. Customize the text formatting (optional).
  4. Save the document in .doc or .docx format using the Document.SaveToFile() method.
from spire.doc import *

# Create a new Word document
doc = Document()
section = doc.AddSection()
section.PageSetup.Margins.All = 72.0

# Add text to the document
paragraph = section.AddParagraph()
text = paragraph.AppendText("This is a sample paragraph.")

# Apply font formatting
character_format = CharacterFormat(doc)
character_format.FontName = "Times New Roman"
character_format.FontSize = 12.0
text.ApplyCharacterFormat(character_format)

# Save the document to a .docx or .doc file
doc.SaveToFile("TextToWord.docx", FileFormat.Docx2019)
# doc.SaveToFile("TextToWord.doc", FileFormat.Doc)
doc.Close()

Converting TXT Files to Word

To convert a text (.txt) file to a Word document:

  1. Open the text file using the Document class.
  2. Save the content to a .doc or .docx file using the Document.SaveToFile() method.
from spire.doc import *

# Open the text file
doc = Document("Example.txt")

# Save the content to a .docx or .doc file
doc.SaveToFile("TextFileToWord.docx", FileFormat.Docx2019)
# doc.SaveToFile("TextFileToWord.doc", FileFormat.Doc)
doc.Close()

Converting Word to Text in Python

Spire.Doc also allows you to convert entire Word documents to text or extract specific text from Word documents, which can be helpful for a variety of tasks, such as parsing documents or exporting content to other formats.

Converting Word Documents to Text

To extract the entire text from a Word document:

  1. Open the Word document using the Document class.
  2. Use the Document.GetText() method to retrieve the text.
  3. Save the extracted text to a .txt file.
from spire.doc import *

# Open the Word document
doc = Document("Sample.docx")

# Extract the entire text from the document
text = document.GetText()

# Save the extracted text to a text file
with open("WordToText.txt", "w", encoding="utf-8") as file:
file.write(text)

doc.Close()

Extracting Specific Text from Word Documents

In addition to extracting the entire text, Spire.Doc also enables extracting specific text from a Word document, such as:

  • Text from certain sections, pages, paragraphs, or lines.
  • Content from tables, headers, footers, and more.

For detailed information on extracting specific text from Word documents, refer to this blog: Extract Text from Word Documents with Python (A Comprehensive Guide).

Conclusion

Converting text to Word or Word to text is a simple yet powerful task that bridges the gap between basic and advanced document formats. By leveraging tools like Spire.Doc, you can automate these conversions quickly and efficiently.

Additional Resource

Spire.Doc for Python Documentation

See Also

Final Words

Thank you for taking the time to read our article. If you found it helpful or enjoyable, feel free to clap for it. We’d appreciate your support!

--

--

Alice Yang
Alice Yang

Written by 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.