-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvoice.py
59 lines (47 loc) · 1.92 KB
/
invoice.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
56
57
58
59
from docxtpl import DocxTemplate, InlineImage #Document updation and adding data
from docx.shared import Mm #Defining size of logo
import fitz #used for converting PDF to image
from docx2pdf import convert #For converting docx to pdf
import os #For removing files
import pythoncom
#Function to make invoice
def invoice(template, context):
pythoncom.CoInitialize()
document = DocxTemplate(template)
#Datas inside the Invoice
#Customize as required. The name of the keys in the values must be same as the keys in the invoice.
#The values must follow Order.
# context = {
# 'logo': InlineImage(document, logo, width=Mm(25)),#To set Logo or images in the invoice.
# 'date': date,
# 'invoice_number': invoice_number,
# 'company_address': company_address,
# 'invoice_to': invoice_to,
# 'amount': amount
# }
context = context
#Rendering Document
document.render(context)
#Saving Document
document.save(f'{context['invoice_no']}-invoice.docx')
#Converting docx to pdf
convert(f'{context['invoice_no']}-invoice.docx', f'{context['invoice_no']}-invoice.pdf')
os.remove(f'{context['invoice_no']}-invoice.docx')
pythoncom.CoUninitialize()
# #Converting PDF to Image
# with fitz.open(f'{name}.pdf') as pdf_doc:
# for pg in range(pdf_doc.page_count):
# page = pdf_doc.load_page(pg)
# #Scalling Resolution of Image
# mat = fitz.Matrix(2, 2)
# pix = page.get_pixmap(matrix=mat)
# image = f"{name}.png"
# pix.save(image)
# print(f"Saved {image}")
# #Removing Temp Files
# if image: #f'{name}.png': #in os.listdir():
# os.remove(f'{name}.docx')
# os.remove(f'{name}.pdf')
# return image
#Tesing Function
# invoice("invoice.docx", "demo", "image.png", "July 8, 2019", "1234", "company address", "invoice to address", "1000.00")