python docx module

Solutions on MaxInterview for python docx module by the best coders in the world

showing results for - "python docx module"
Mariana
16 Oct 2018
1from docx import Document
2from docx.shared import Inches
3
4document = Document()
5
6document.add_heading('Document Title', 0)
7
8p = document.add_paragraph('A plain paragraph having some ')
9p.add_run('bold').bold = True
10p.add_run(' and some ')
11p.add_run('italic.').italic = True
12
13document.add_heading('Heading, level 1', level=1)
14document.add_paragraph('Intense quote', style='Intense Quote')
15
16document.add_paragraph(
17    'first item in unordered list', style='List Bullet'
18)
19document.add_paragraph(
20    'first item in ordered list', style='List Number'
21)
22
23document.add_picture('monty-truth.png', width=Inches(1.25))
24
25records = (
26    (3, '101', 'Spam'),
27    (7, '422', 'Eggs'),
28    (4, '631', 'Spam, spam, eggs, and spam')
29)
30
31table = document.add_table(rows=1, cols=3)
32hdr_cells = table.rows[0].cells
33hdr_cells[0].text = 'Qty'
34hdr_cells[1].text = 'Id'
35hdr_cells[2].text = 'Desc'
36for qty, id, desc in records:
37    row_cells = table.add_row().cells
38    row_cells[0].text = str(qty)
39    row_cells[1].text = id
40    row_cells[2].text = desc
41
42document.add_page_break()
43
44document.save('demo.docx')
45