Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8430c4a

Browse files
authored
Add files via upload
1 parent fcbbd54 commit 8430c4a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

write-to-word/readme.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
You can read tutorial https://www.roytuts.com/a-guide-to-write-word-file-using-python/

write-to-word/simple.docx

38.7 KB
Binary file not shown.

write-to-word/write_docx.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from docx import Document
2+
from docx.shared import Inches
3+
4+
document = Document()
5+
6+
document.add_heading('Document Title', 0)
7+
8+
p = document.add_paragraph('A plain paragraph having some ')
9+
p.add_run('bold').bold = True
10+
p.add_run(' and some ')
11+
p.add_run('italic.').italic = True
12+
13+
document.add_heading('Heading, level 1', level=1)
14+
15+
document.add_paragraph('Intense quote', style='Intense Quote')
16+
17+
document.add_paragraph(
18+
'first item in unordered list', style='List Bullet'
19+
)
20+
21+
document.add_paragraph(
22+
'first item in ordered list', style='List Number'
23+
)
24+
25+
document.add_picture('C:\\Users\\soumi\\Pictures\\application.jpg', width=Inches(1.25))
26+
27+
28+
recordset = [
29+
{
30+
"id" : 1,
31+
"qty": 2,
32+
"desc": "New item"
33+
},
34+
{
35+
"id" : 2,
36+
"qty": 2,
37+
"desc": "New item"
38+
},
39+
{
40+
"id" : 3,
41+
"qty": 2,
42+
"desc": "New item"
43+
}
44+
]
45+
print(recordset[0]['id'])
46+
table = document.add_table(rows=1, cols=3)
47+
hdr_cells = table.rows[0].cells
48+
hdr_cells[0].text = 'Id'
49+
hdr_cells[1].text = 'Quantity'
50+
hdr_cells[2].text = 'Description'
51+
for item in recordset:
52+
#print(item)
53+
row_cells = table.add_row().cells
54+
row_cells[0].text = item['id']
55+
row_cells[1].text = item['qty']
56+
row_cells[2].text = item['desc']
57+
58+
document.add_page_break()
59+
60+
document.save('simple.docx')

0 commit comments

Comments
 (0)