Generating Word documents
Never hand-write .docx with write_file — it's a zipped XML format and will corrupt. Build it with python-docx instead.
pip install python-docx
from docx import Document
doc = Document()
doc.add_heading("Title", level=1)
doc.add_paragraph("Body text.")
doc.add_heading("Section", level=2)
doc.add_paragraph("More text.", style="List Bullet")
doc.save("/home/user/report.docx")
Write this as a script with write_file, run it with shell, then present_file the .docx — never the script.
For tables:
table = doc.add_table(rows=1, cols=3)
table.style = "Light Grid Accent 1"
hdr = table.rows[0].cells
hdr[0].text, hdr[1].text, hdr[2].text = "Name", "Value", "Notes"
For a cover page or letterhead, add a page break with doc.add_page_break() and keep the first page minimal.