Hi, If you need to create a word document for an output of your python code, let’s say and exercise for your pupils, here is what you got to do.
So let’s say we want to give a task that is different for each boy and girl in the classroom, let’s say a different equation of the second degree.
First we need a module,
Searching on the net, I found that you can use the
docx module
- Install it with:
pip install python-docx
- from the command line
- If you need it, install also the lxml module
The do this code in the python interpreter:
from docx import Document from docx.shared import Inches
doc = Document() doc.add_heading("Viva Python",0) par = doc.add_paragraph("It's very nice to code in Python with docx.") par.add_run("This text is bold.").bold = True par.add_run("This one, instead, is italic").italic = True doc.save("first.docx")
So, we have learned some basic stuff it is quite easy, don’t it?
In the next episode we will checkout lists of items and tables, as shown in the documentation of the site of python-docx.See the page of docx for further documentation
Comments
Post a Comment