-
Notifications
You must be signed in to change notification settings - Fork 0
/
Barcode Generator 2.py
377 lines (233 loc) · 10.4 KB
/
Barcode Generator 2.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget,QPushButton,QFileDialog,QLineEdit,QHBoxLayout
from PyQt5.QtGui import QPixmap, QFont,QIcon
from PyQt5.QtCore import Qt
import pandas as pd
import xlsxwriter
import os
import re
from io import BytesIO
import barcode
from barcode.writer import ImageWriter
import requests
import firebase_admin
from firebase_admin import credentials,storage
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QFileDialog
import shutil
cred=credentials.Certificate("techie-9eft-firebase-adminsdk-v98cy-9e1660e817.json")
firebase_admin.initialize_app(cred,{'storageBucket': 'techie-9eft.appspot.com'})
def downloadFile(Input,path):
if Input=="text":
# Specify the path and filename of the file to be downloaded
# Prompt the user to select the download location
save_path, _ = QFileDialog.getSaveFileName(None, "Save File", "", "Png (*.png)")
if save_path:
try:
shutil.copy(path, save_path)
print("File downloaded successfully.")
os.remove(path)
except Exception as e:
print("Error while downloading file:", str(e))
else:
print("Download canceled.")
elif Input=="upload":
save_path, _ = QFileDialog.getSaveFileName(None, "Save File", "", "Excel files (*.xlsx)")
if save_path:
try:
shutil.copy(path, save_path)
print("File downloaded successfully.")
os.remove(path)
except Exception as e:
print("Error while downloading file:", str(e))
else:
print("Download canceled.")
os.remove(path)
def generate_barcode(text, number='',barcode_type='code128'):
text=str(text)
data=pd.read_csv('barcode.csv',index_col=False)
last_row=data.iloc[-1,0]
number=last_row+1
data.loc[len(data.index)]=[number,text]
data.to_csv('barcode.csv',index=False)
file_path='barcode.csv'
bucket = storage.bucket() # storage bucket
blob = bucket.blob(file_path)
blob.upload_from_filename(file_path)
text=text.replace('\n', '')
text=text.replace(' ', '')
text=text.replace(':', '')
pattern = r'[^a-zA-Z0-9\s]' # Keep alphabets, numbers, and whitespaces
# Use the sub() function from the re module to remove non-text characters
text=text.replace('NaN','')
cleaned_text = re.sub(pattern, '', text)
'''
# Create a barcode object
barcode_class = barcode.get_barcode_class(barcode_type)
barcode_object = barcode_class(text, writer=ImageWriter,add_checksum=False)'''
print(str(number))
barcode_object=barcode.EAN13(str(number))
barcode_object.writer=ImageWriter()
image=barcode_object.render()
filename = f'barcode_{text}'
image.save(filename+'.png')
# Set the filename for the barcode image
return filename+".png"
if getattr(sys, 'frozen', False):
# If the application is run as a bundle (e.g., PyInstaller executable)
logo_path = sys._MEIPASS + '/logo.png' # Update 'logo.png' to the name of your logo file
else:
# If the application is run as a standalone script
logo_path = 'logo.png'
# Get the current directory
current_dir = os.path.dirname(os.path.abspath(__file__))
# Set the current directory
os.chdir(current_dir)
app = QApplication(sys.argv)
def window_1(app):
# Create a QMainWindow
window = QMainWindow()
window.setWindowTitle("Bar Code Generator")
window.setFixedSize(700, 400)
window.setWindowIcon(QIcon('logo.ico'))
# Create a QVBoxLayout to hold the QLabel and text label
layout = QVBoxLayout()
# Add the image QLabel
image_label = QLabel()
pixmap = QPixmap("logo.png") # Replace with your image path
image_label.setPixmap(pixmap)
image_label.setAlignment(Qt.AlignCenter)
layout.addWidget(image_label)
# Add the text QLabel
text_label = QLabel("Unlock Your Products' Potential with Seamless Bar Code Generation!")
text_label.setAlignment(Qt.AlignCenter)
text_label.setFont(QFont("Arial", 14)) # Increase font size (adjust as needed)
text_label.setMargin(1) # Adjust the margin around the text label
layout.addWidget(text_label)
# Create a QWidget to hold the layout
central_widget = QWidget()
central_widget.setLayout(layout)
window.setCentralWidget(central_widget)
#add button
button_next=QPushButton("Next")
button_close=QPushButton("Close")
#add then to layout
layout.addWidget(button_next)
layout.addWidget(button_close)
#Add color
button_close.setStyleSheet("background-color: #436953;color:white;")
button_next.setStyleSheet("background-color: #436953;color:white;")
#ADD button functionality
def button_function():
sender = window.sender()
if sender == button_close:
window.close()
elif sender == button_next:
window.close()
window_2(app)
button_next.clicked.connect(button_function)
button_close.clicked.connect(button_function)
# Show the main window
window.show()
# Start the application event loop
sys.exit(app.exec_())
def window_2(app):
# Create a QMainWindow
window = QMainWindow()
window.setWindowTitle("Bar Code Generator")
window.setFixedSize(700, 400)
window.setWindowIcon(QIcon('logo.ico'))
layout = QVBoxLayout()
central_widget = QWidget()
central_widget.setLayout(layout)
window.setCentralWidget(central_widget)
# Add a label for the instruction on Uploading
text_entry = QLineEdit(window)
#Add styles for all the widgets
text_entry.setFixedHeight(100)
layout.addWidget(text_entry)
# Function for tgetting the text
def get_text():
sender=window.sender()
if sender==submit_text:
entered_text = text_entry.text()
image_name=generate_barcode(entered_text)
Input="text"
downloadFile(Input,image_name)
upload_button = QPushButton("Upload File", window)
upload_button.setGeometry(250, 400, 200, 50)
upload_button.setStyleSheet("background-color:#436953;color:white;")
# Add submit button for text-based input
submit_text=QPushButton("Generate",window)
submit_text.setStyleSheet("background-color:#436953;color:white;")
submit_text.setFixedWidth(200)
layout.addWidget(submit_text)
submit_text.clicked.connect(lambda:get_text())
# Set width for the QPushButton widget
upload_button.setFixedWidth(200)
# Create a QVBoxLayout to hold the QLabel and text label
layout.addWidget(upload_button)
def upload():
sender=window.sender()
if sender==upload_button:
file_dialog = QFileDialog()
file_path, _ = file_dialog.getOpenFileName(window, "Upload File")
if file_path:
# Process the selected file
data = pd.read_excel(file_path)
image_list = []
if "Unnamed" in ''.join(data.columns):
number=1
else:
number=2
# Generate images and store their paths
for index, row in data.iloc[0:].iterrows():
image_name = generate_barcode(str(row.to_string(index=False)),str(number))
image_list.append(image_name)
number+=1
# Create the result workbook and worksheet
workbook = xlsxwriter.Workbook("result.xlsx")
worksheet = workbook.add_worksheet()
if ''.join(data.columns) in image_list[0]:
image_list.remove(image_list[0])
elif 'Unnamed'in ''.join(data.columns):
image_list.remove(image_list[0])
print(image_list)
# Iterate over the images and insert them into the worksheet
for i, image in enumerate(image_list):
filename = image
print(filename)
file = open(filename, 'rb')
data_ = BytesIO(file.read())
file.close()
# Adjust the cell reference based on the current iteration
cell_reference = f'A{2 + i}' # A1, A2, A3, ...
worksheet.insert_image(cell_reference, filename, {'image_data': data_})
#remove the images now
for c in image_list:
os.remove(c)
workbook.close()
import openpyxl
from openpyxl.utils.dataframe import dataframe_to_rows
file_path = 'result.xlsx'
wb = openpyxl.load_workbook(file_path)
# Step 2: Select the worksheet where you want to insert the data
sheet_name = 'Sheet1' # Replace 'Sheet1' with the actual sheet name
ws = wb[sheet_name]
# Step 3: Determine the starting cell for inserting the DataFrame
start_row = ws.max_row + 1 # Start from the next available row
# Step 4: Convert the DataFrame to rows
if "Unnamed" in ''.join(data.columns):
rows = list(dataframe_to_rows(data, index=False, header=False))
else:
rows = list(dataframe_to_rows(data, index=False, header=True))
# Step 5: Insert the rows into the worksheet
for row in rows:
ws.append(row)
# Step 6: Save the modified Excel file
wb.save(file_path)
filepath="result.xlsx"
Input="upload"
downloadFile(Input,filepath)
upload_button.clicked.connect(upload)
window.show()
window_1(app)