-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
156 lines (115 loc) · 5.8 KB
/
main.py
File metadata and controls
156 lines (115 loc) · 5.8 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import datetime as dt
import subprocess
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import docx
class InvoiceGen:
def __init__(self):
self.root = tk.Tk()
self.root.title('InvoiceGen 1.1')
self.root.geometry('500x600')
self.partner_label = tk.Label(self.root, text='Partner')
self.partner_street_label = tk.Label(self.root, text='Partner Street')
self.partner_zip_city_country_label = tk.Label(self.root, text='Partner Zip_City_Country')
self.invoice_number_label = tk.Label(self.root, text='Invoice Number')
self.service_description_label = tk.Label(self.root, text='Service Description')
self.service_amount_label = tk.Label(self.root, text='Amount')
self.service_single_price_label = tk.Label(self.root, text='Single Price')
self.payment_method_label = tk.Label(self.root, text='Payment Method')
self.payment_options = {
'Main Bank':{
'Recipient': 'Jeff Muney',
'Bank': 'PesaMingi',
'Branch':'KonaDongo',
'Account':'1223 4578 8900 7367'
},
'MPESA':{
'Recipient': 'Pita Nuako',
'Bank': 'MPESA',
'Branch':'NA',
'Account':'NAMBA YANGU'
},
'Second Bank':{
'Recipient': 'Bestprof',
'Bank': 'Manibank',
'Branch':'Nairobe',
'Account':'1223 4578 8900 7367'
}
}
self.partner_entry = tk.Entry(self.root)
self.partner_street_entry = tk.Entry(self.root)
self.partner_zip_city_country_entry = tk.Entry(self.root)
self.invoice_number_entry = tk.Entry(self.root)
self.service_description_entry = tk.Entry(self.root)
self.service_amount_entry = tk.Entry(self.root)
self.service_single_price_entry = tk.Entry(self.root)
self.payment_method_entry = tk.Entry(self.root)
self.payment_option = tk.StringVar(self.root)
self.payment_option.set('Main Bank')
self.payment_method_dropdown = tk.OptionMenu(self.root, self.payment_option, "Main Bank","MPESA","Second Bank")
self.make_button = tk.Button(self.root, text='Generate Invoice', command=self.generate_invoice)
padding_options={'fill':'x', 'expand':True, 'padx':5, 'pady': 2}
self.partner_label.pack(padding_options)
self.partner_entry.pack(padding_options)
self.partner_street_label.pack(padding_options)
self.partner_street_entry.pack(padding_options)
self.partner_zip_city_country_label.pack(padding_options)
self.partner_zip_city_country_entry.pack(padding_options)
self.invoice_number_label.pack(padding_options)
self.invoice_number_entry.pack(padding_options)
self.service_description_label.pack(padding_options)
self.service_description_entry.pack(padding_options)
self.service_amount_label.pack(padding_options)
self.service_amount_entry.pack(padding_options)
self.service_single_price_label.pack(padding_options)
self.service_single_price_entry.pack(padding_options)
self.payment_method_label.pack(padding_options)
self.payment_method_dropdown.pack(padding_options)
self.make_button.pack(padding_options)
#self.payment_method_entry = tk.Entry(self.root)
self.root.mainloop()
@staticmethod
def write_doc(paragraph, old_text, new_text):
if old_text in paragraph.text:
paragraph.text = paragraph.text.replace(old_text, new_text)
def generate_invoice(self):
template = docx.Document(r'Template.docx')
selected_payment_method = self.payment_options[self.payment_option.get()]
try:
replacements ={
"[Date]": dt.datetime.today().strftime('%Y-%m-%d'),
"[Partner]": self.partner_entry.get(),
"[Partner Street]": self.partner_street_entry.get(),
"[Partner Zip_City_Country]": self.partner_zip_city_country_entry.get(),
"[Invoice Number]": self.invoice_number_entry.get(),
"[Service Description]": self.service_description_entry.get(),
"[Amount]": self.service_amount_entry.get(),
"[Single Price]": f"${float(self.service_single_price_entry.get()):.2f}",
"[Full Price]": f'${float(self.service_amount_entry.get())*float(self.service_single_price_entry.get()):2f}',
"[Recipient]": selected_payment_method['Recipient'],
"[Bank]": selected_payment_method['Bank'],
"[Branch]": selected_payment_method['Branch'],
"[Account]": selected_payment_method['Branch'],
}
except ValueError:
messagebox.showerror('Error', 'Invalid amount or price!')
return
for paragraph in list(template.paragraphs):
for old_text, new_text in replacements.items():
self.write_doc(paragraph, old_text, new_text)
for table in template.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
for old_text, new_text in replacements.items():
self.write_doc(paragraph, old_text, new_text)
save_path = filedialog.asksaveasfilename(defaultextension='pdf', filetypes=[('PDF documents', '*.pdf')])
template.save('Template.docx')
import docx2pdf
from docx2pdf import convert
convert("Template.docx", save_path)
messagebox.showinfo('Success', 'Invoice created and saved successfully!')
if __name__ == '__main__':
InvoiceGen()