How can i convert html to word docx in python?

Jayasri Tanneru picture Jayasri Tanneru · Mar 14, 2017 · Viewed 19.3k times · Source
import pypandoc
output = pypandoc.convert_file('file.html', 'docx', outputfile="file1.docx")
assert output == ""

It is generating new docx file but ignoring styles.

Can any one tell me how to generate new docx file with styles?

Thanks in advance for your answers.

Answer

Emin Mastizada picture Emin Mastizada · Mar 14, 2017

In Windows the easiest way will be to use MS Word using pywin32 plugin. Here is good answer with example code.

Using pypandoc:

output = pypandoc.convert(source='/path/to/file.html', format='html', to='docx', outputfile='/path/to/output.docx', extra_args=['-RTS'])

Read this for extra_args.