I have a structured text on python. I want to save it to a docx file.
Something like
text = "A simple text\n Structured"
with open('docx_file.docx', 'w') as f:
f.write(text)
Check python-docx package:
from docx import Document
document = Document()
document.add_heading('A simple text', level=1)
document.add_paragraph('some more text ... ')
document.save('docx_file.docx')
A docx file is not a plain text file, so unless you want to not use a library for this, I recommend https://grokonez.com/python/how-to-read-write-word-docx-files-in-python-docx-module. Unless you need to use a "fancy" format like docx, I would recommend just writing plain text to a txt file.
Here is the code that I have used to save a text string to a word document.
newfile = open("Result.doc", "w+")
newfile.write("Hello World!")
newfile.close()