Effortlessly Convert Between Text and Word DOC or DOCX Formats Using Python
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?
- Tool for Converting Between Text and Word in Python
- Converting Text to Word in Python
- Converting Plain Text to Word
- Converting TXT Files to Word - Converting Word to Text in Python
- Converting Word Documents to Text
- Extracting Specific Text from Word Documents
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.
- 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. - 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. - 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.DocFor 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:
- Create a new Word document using the Document class.
- Use the Paragraph.AppendText() method to add text.
- Customize the text formatting (optional).
- 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:
- Open the text file using the Document class.
- 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:
- Open the Word document using the Document class.
- Use the Document.GetText() method to retrieve the text.
- 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
- Create or Extract Tables in Word with Python
- Add or Extract Headings in Word Documents with Python
- Extract Images from Word Documents with Python
- Find and Replace Text in Word DOC or DOCX Documents with Python
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!










