3 Steps to Convert Text to Word or Word to Text in Python

·

4 min read

The conversion between text and Word documents is common in both business and life. For instance, you might need to create a well-structured Word report with images and text from a plain text file or extract only the text from a Word document for lightweight storage and analysis.

While manual formatting can be time-consuming, Python offers a quick and efficient solution. This article will guide you through how to convert text to Word or Word to text using Python, complete with step-by-step instructions and practical code examples to streamline your workflow.


Python Library to Apply the Conversion

When it comes to finishing tasks using Python, many people will use some third-party libraries, such as Spire.Doc for Python, Aspose.Words for Python via .NET, python-docx of PyPI, and so on. Among them, Spire.Doc (short for Spire.Doc for Python) is recommended for intuitive APIs, regular updates, and timely technical support. You can install it using the pip command: pip install Spire.Doc.

Convert Text to Word Doc Files in Three Steps

When you want to enrich a text file with images, hyperlinks, and other elements, converting it to a Word document will be very helpful. Luckily, the Document.SaveToFile() method provided by Spire.Doc allows you to convert a text file to a Word file in just three steps, let’s take a closer look.

Steps to convert Text to Word Doc:

  • Create an object of the Document class.

  • Read a Text document from the local storage using the Document.LoadFromFile() method.

  • Convert the text file to Word through the Document.SaveToFile() method.

Here is the code example of transforming a Text file into a Word file:

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

# Create a Document object
document = Document()

# Load a TXT file
document.LoadFromFile("/sample.txt")

# Save the TXT file as Word
document.SaveToFile("/TxtToWord.docx", FileFormat.Docx2016)
document.Close()

Convert a Text File to a Word Doc

Quickly Convert Doc Files to Text Files

Saving a Word document as a text file is often the most efficient way to store information, as it significantly reduces file size and simplifies text analysis. With the Document.SaveToFile() method, you can easily perform this conversion in just three steps. Below, we provide detailed instructions and code examples—let’s dive in!

Steps to convert a Doc file to a text file:

  • Create an instance of the Document class.

  • Load a Word document from files through the Document.LoadFromFile() method.

  • Convert the Word document to plain text using the Document.SaveToFile() method.

Below is an example of converting a Word doc to a text file:

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

# Create a Document object
document = Document()

# Load a Word file from disk
document.LoadFromFile("/sample.docx")

# Save the Word file in txt format
document.SaveToFile("/WordToTxt.txt", FileFormat.Txt)
document.Close()

Convert Word Doc Files to Text Files

How to Convert a Specific Paragraph in Word to Text

After reviewing the previous chapters, you might want to explore how to convert specific paragraphs from a Word document into a plain text file. Accomplishing this task with Python is straightforward, but the approach differs from earlier methods. The process begins by locating the desired section, selecting the specific paragraph, and then extracting its content as text. Below are the detailed steps to guide you through.

Steps to convert paragraphs of Word documents to text:

  • Create a Document object.

  • Load a Word file from the file path using the Document.LoadFromFile() method.

  • Access a section with the Document.Sections.get_Item() method.

  • Get a specific paragraph of the section through the Paragraphs.get_Item() method.

  • Extracting text of the paragraph using the Paragraph.Text property, and write the text as a text file.

Here is the code example of converting the first paragraph of the first section in a Word document to text:

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

# Create a Document object
document = Document()

# Load a Word file into the Document object
document.LoadFromFile("/AI-Generated Art.docx")

# Get the first section of the document
sec = document.Sections.get_Item(0)

# Get the second paragraph in the first section
para = sec.Paragraphs.get_Item(1)

# Extract the text content of the paragraph
text = para.Text

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

# Release resources used by the Document object
document.Close()

Convert Paragraphs in Word Files to Text Files

The Bottom Line

This page demonstrates how to convert between text and Word using Python, with additional instructions for saving specific paragraphs as text. Detailed steps and code examples are included for reference. By the end of this guide, you'll find that converting between text and Word is quick and easy!