Python Program to Convert TXT File to DOCX

2025-11-27 python docx text-to-docx python-docx converter file-tools utilities

A Python program that converts any text (.txt) file into a formatted Word (.docx) document using the python-docx library. Each line is added as a paragraph, making it useful for converting notes, logs, or raw text into an editable document.

Explanation

A TXT to DOCX Converter is a simple Python program that takes a plain text file and converts it into a Microsoft Word document (.docx). This is helpful when you want to convert notes, logs, plain text data, exported content, or any text-based file into a professional and editable Word format.

This program uses the python-docx library, which allows Python to create and edit Word documents. Each line of the text file becomes a separate paragraph inside the DOCX file, preserving readability and formatting.

Requirements

  • pip install python-docx

    Installs the python-docx library required for creating Word (.docx) files.

  • Python version 3 or higher.
  • A valid text (.txt) file encoded in UTF-8.
  • DOCX output name must end with .docx.
  • Program converts each line into a paragraph inside the DOCX.

Code Explanation

from docx import Document

Imports the Document class which is used to create and edit DOCX files.

print("=== TXT to DOCX Converter ===")

Prints the program title for clarity.

input_txt = input("Enter text file path: ").strip()

Takes user input for the text (.txt) file that needs to be converted.

output_docx = input("Enter output DOCX name: ").strip() or "output.docx"

Gets the output DOCX filename. Uses output.docx if none provided.

doc = Document()

Creates a new empty DOCX document where text will be stored.

with open(input_txt, "r", encoding="utf-8", errors="replace") as file:
    for line in file:
        doc.add_paragraph(line.rstrip())

Opens the text file safely using UTF-8 encoding.
Each line of the text file becomes a separate paragraph in the DOCX document.

doc.save(output_docx)

Saves the DOCX file with the provided output filename.

print(f"DOCX created successfully: {output_docx}")

Prints a confirmation message showing the output file name.

Key Points

  • Simple conversion of text files into editable DOCX format.
  • Each line becomes a paragraph in Word document.
  • Uses the python-docx library.
  • Useful for converting logs, notes, exports, and plain text data.
  • Works with UTF-8 encoded text files.

Full Python Program

from docx import Document

print("=== TXT to DOCX Converter ===")

input_txt = input("Enter text file path: ").strip()
output_docx = input("Enter output DOCX name: ").strip() or "output.docx"

doc = Document()

with open(input_txt, "r", encoding="utf-8", errors="replace") as file:
    for line in file:
        doc.add_paragraph(line.rstrip())

doc.save(output_docx)

print(f"DOCX created successfully: {output_docx}")

Output :

> py txtToDocx.py

=== TXT to DOCX Converter ===
Enter text file path: sample.txt
Enter output DOCX name: notes.docx

DOCX created successfully: notes.docx
1