How to transfrom a txt file in a doc file

From txt to docx

Installing python-docx module

pip install python-docx

you can transform easily a txt file in a docx file with this code:

"""Transform txt in docx.

Giovanni Gatto
"""

from docx import Document
import glob
import os


doc = Document()

files = glob.glob("*.txt")
print(files)
file = input("Scrivi il nome del file senza .txt: ") + ".txt"

with open(file, 'r', encoding='utf-8') as openfile:
	line = openfile.read()
	doc.add_paragraph(line)
	doc.save(file + ".docx")

os.system(file + ".docx")

 

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.