forked from Vnessah/docx-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
55 lines (48 loc) · 1.52 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import io
from docx.shared import Cm
from docxtpl import DocxTemplate, InlineImage
def get_context():
""" You can generate your context separately since you may deal with a lot
of documents. You can carry out computations, etc in here and make the
context look like the sample below.
"""
return {
'invoice_no': 12345,
'date': '30 Mar',
'due_date': '30 Apr',
'name': 'Jane Doe',
'address': '123 Quiet Lane',
'subtotal': 335,
'tax_amt': 10,
'total': 345,
'amt_paid': 100,
'amt_due': 245,
'row_contents': [
{
'description': 'Eggs',
'quantity': 30,
'rate': 5,
'amount': 150
}, {
'description': 'All Purpose Flour',
'quantity': 10,
'rate': 15,
'amount': 150
}, {
'description': 'Eggs',
'quantity': 5,
'rate': 7,
'amount': 35
}
]
}
def from_template(template, signature):
template = DocxTemplate(template)
context = get_context() # gets the context used to render the document
img_size = Cm(7) # sets the size of the image
sign = InlineImage(template, signature, img_size)
context['signature'] = sign # adds the InlineImage object to the context
target_file = io.BytesIO()
template.render(context)
template.save(target_file)
return target_file