Skip to content

Commit 862574d

Browse files
committed
Added QR Ticket Generator from Excel File
1 parent eb5bbca commit 862574d

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed

QR Ticket Generator/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#QR Ticket Generator from Excel File
2+
3+
This project is a QR ticket generator that converts data from an Excel file into QR codes for ticket generation.
4+
5+
6+
###Features
7+
8+
9+
1. Read data from an Excel file.
10+
11+
2. Generate QR codes based on the data.
12+
13+
3. Save QR codes as image files.
14+
15+
4. Print tickets with QR codes.
16+
17+
###Contributing
18+
19+
Contributions are welcome! If you have any ideas, suggestions, or bug reports, please open an issue or submit a pull request.
20+

QR Ticket Generator/main.py

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import qrcode
2+
import pandas as pd
3+
from email.mime.multipart import MIMEMultipart
4+
from email.mime.text import MIMEText
5+
from email.mime.application import MIMEApplication
6+
import smtplib
7+
import os
8+
from fpdf import FPDF
9+
# gmail auth
10+
gmail_user = 'Event.vitb@gmail.com'
11+
gmail_password = 'jydbwqtbfvwwrdpv'
12+
13+
FILEPATH = "InsertDataHERE.xlsx"
14+
15+
16+
def send_ticket(email, full_name, registration_number, event_name, pdf_dir):
17+
try:
18+
msg = MIMEMultipart()
19+
msg['From'] = gmail_user
20+
msg['To'] = email
21+
msg['Subject'] = 'Event Ticket'
22+
# Add text to the email body
23+
text = MIMEText(f"Hey {full_name},"
24+
f"\n PFA of your event {event_name} Ticket"
25+
f"\n We hope you enjoy the event!")
26+
msg.attach(text)
27+
# # Add an image attachment to the email
28+
# with open('image.jpg', 'rb') as f:
29+
# img = MIMEImage(f.read())
30+
# msg.attach(img)
31+
# Add a pdf attachment to the email
32+
fileName = pdf_dir
33+
with open(fileName, 'rb') as f:
34+
pdf = MIMEApplication(f.read(), _subtype='pdf')
35+
pdf.add_header('content-disposition', 'attachment', filename=os.path.basename(fileName))
36+
msg.attach(pdf)
37+
# Create the SMTP server and send the email
38+
server = smtplib.SMTP('smtp.gmail.com', 587)
39+
server.starttls()
40+
server.login(gmail_user, gmail_password)
41+
server.send_message(msg)
42+
server.quit()
43+
44+
except:
45+
print(f"{registration_number}-{full_name} Failed to send mail")
46+
47+
48+
def make_qr(full_name: str, registration_number: str, unique_id: str, event_name: str):
49+
try:
50+
51+
print(f"{registration_number}-{full_name} QR successfully generated")
52+
53+
# Making QR code
54+
qr = qrcode.QRCode(version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4)
55+
qr.add_data(unique_id)
56+
qr.make(fit=True)
57+
58+
# Saving QR in respective directory
59+
img = qr.make_image(fill_color="black", back_color="white")
60+
qrcode_dir = f"{registration_number}.png"
61+
img.save(qrcode_dir)
62+
63+
print(f"{registration_number}-{full_name} QR Code saved here:- {qrcode_dir}")
64+
65+
return qrcode_dir
66+
except:
67+
print(f"{registration_number}-{full_name} Failed to generate QR for ")
68+
69+
70+
class PDF(FPDF):
71+
def __init__(self):
72+
super().__init__(format='A5', orientation='L')
73+
74+
def header(self):
75+
# Arial bold 15
76+
self.set_font('Arial', 'B', 15)
77+
# Move to the right
78+
self.cell(80)
79+
# Title
80+
self.cell(30, 10, 'Entry Pass')
81+
# Line break
82+
self.ln(20)
83+
84+
def heading(self, EVENT_NAME):
85+
self.cell(80)
86+
# Font
87+
self.set_font("Arial", 'B', 40)
88+
# Event name
89+
self.cell(30, 30, EVENT_NAME, 0, 0, 'C')
90+
91+
def date_time_venue(self, DATE_TIME_EVENT, VENUE_EVENT):
92+
self.cell(40)
93+
# Font
94+
self.set_font("Arial", 'B', 10)
95+
# DATE_TIME_VENUE
96+
self.cell(-110, 76, DATE_TIME_EVENT, 0, 0, 'C')
97+
self.cell(110, 88, VENUE_EVENT, 0, 0, 'C')
98+
99+
def attendee_name(self, PER_NAME):
100+
self.set_y(70)
101+
self.set_font("Arial", 'B', 25)
102+
# ATTENDEE NAME
103+
self.cell(60, 50, PER_NAME, 0, 0, 'L')
104+
# self.cell(65, 58, ROLE, 0, 0, 'C')
105+
106+
def role_in_event(self, ROLE):
107+
self.set_y(76)
108+
self.set_font("Arial", 'I', 10)
109+
# Role in Event
110+
self.cell(60, 52, f"ROLE: {ROLE}", 0, 0, 'L')
111+
112+
def payment_status(self, PAYMENT):
113+
self.set_y(-37)
114+
self.set_font("Arial", '', 10)
115+
# Payment Status
116+
self.cell(0, 10, PAYMENT, 0, 0, 'L')
117+
118+
def order_number(self, order_no):
119+
self.set_x(-75)
120+
self.set_font("Arial", '', 10)
121+
# Order Number
122+
self.cell(0, 10, f"Order Number: {order_no}", 0, 0, 'L')
123+
124+
def get_qr_code(self, file_path: str):
125+
self.image(name=file_path, x=155, y=85, w=25, h=25)
126+
127+
# Page footer
128+
def footer(self):
129+
# Position at 1.5 cm from bottom
130+
self.set_y(-15)
131+
# Arial italic 8
132+
self.set_font('Arial', 'I', 8)
133+
# Page number
134+
self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
135+
136+
137+
def generate_pdf(event_name: str, full_name: str, role: str, payment_status: str,
138+
unique_id: str, qrcode_dir: str, registration_number: str,
139+
date_time_event='28 February 2023, 4PM to 7PM', venue_event='location_hehe'):
140+
try:
141+
142+
pdf = PDF()
143+
pdf.alias_nb_pages()
144+
pdf.add_page()
145+
pdf.set_font('Times', '', 12)
146+
pdf.heading(EVENT_NAME=event_name)
147+
pdf.date_time_venue(DATE_TIME_EVENT=date_time_event, VENUE_EVENT=venue_event)
148+
pdf.attendee_name(PER_NAME=full_name)
149+
pdf.role_in_event(ROLE=role)
150+
pdf.payment_status(PAYMENT=payment_status)
151+
pdf.order_number(order_no=unique_id)
152+
pdf.get_qr_code(qrcode_dir)
153+
154+
pdf_dir = f"{registration_number}.pdf"
155+
156+
pdf.output(pdf_dir)
157+
158+
print(f"{registration_number}-{full_name} PDF saved here:- {pdf_dir}")
159+
os.remove(f"{registration_number}.png")
160+
return pdf_dir
161+
162+
except Exception as e:
163+
print(e)
164+
165+
166+
def start_entry_process():
167+
# collection = connectToDatabase()
168+
df = pd.read_excel(FILEPATH)
169+
confirm_payment_df = df[df['payment status'] == 'captured']
170+
failed_payment_df = df[df['payment status'] == 'failed']
171+
172+
for index in range(0, len(confirm_payment_df)):
173+
# try:
174+
175+
registration_number = confirm_payment_df['registration_number'].iloc[index].upper()
176+
unique_id = confirm_payment_df['order_id'].iloc[index]
177+
event_name = confirm_payment_df['payment button title'].iloc[index]
178+
email = confirm_payment_df['email'].iloc[index]
179+
full_name = confirm_payment_df['full_name'].iloc[index].upper()
180+
gender = confirm_payment_df['gender'].iloc[index].upper()
181+
role = "ATTENDEE"
182+
payment_status = "PAID"
183+
document = {
184+
"registration_number": registration_number,
185+
"full_name": full_name,
186+
"unique_id": unique_id,
187+
"event_name": event_name,
188+
"email": email,
189+
"gender": gender,
190+
"role": role,
191+
"payment_status": payment_status,
192+
"isInside": False
193+
}
194+
try:
195+
196+
qrcode_dir = make_qr(full_name, registration_number, unique_id, event_name)
197+
pdf_dir = generate_pdf(event_name, full_name, role, payment_status,
198+
unique_id, qrcode_dir, registration_number)
199+
200+
send_ticket(email, full_name, registration_number, event_name, pdf_dir)
201+
202+
except Exception as e:
203+
print(e)
204+
print(f"{registration_number}-{full_name} Already in database")
205+
206+
207+
if __name__ == '__main__':
208+
start_entry_process()

QR Ticket Generator/requirements.txt

464 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)