Answer the question
In order to leave comments, you need to log in
How to write data from Python to docx file?
Hello. I am writing a small useful program for my friend. It requires input from the user, and then performs calculations based on them and produces a result. The received data, together with the result, is written to a file from which it will be possible to print them.
I can write to a notepad (.txt) through the program. But if I want to write this into a MS Word(.docx) file, then this file complains about an invalid encoding, it seems.
I searched on the Internet - they suggest importing the docx module and using the methods offered by it. Others talk about saving the data in HTML format, from which word can read it.
Which way should I look?
Thanks in advance for advice and suggestions.
Answer the question
In order to leave comments, you need to log in
python-docx does a great job, it's not clear what prevented you from checking it if you already googled it.
so that there are no encoding problems - use unicode.
# -*- coding: utf-8 -*-
from docx import Document
document = Document()
str=unicode("Тест", "utf-8")
p = document.add_paragraph(str)
document.save('test.docx')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question