Untitled
unknown
plain_text
3 years ago
1.7 kB
12
Indexable
import treepoem
from io import BytesIO
import cups
import os
def list_printers():
conn = cups.Connection()
printers = conn.getPrinters()
return printers
def user_select_printer(printers):
print("List of connected printers:")
for i, printer in enumerate(printers, start=1):
print(f"{i}. {printer}")
selected_printer = int(input("Enter the number of the printer you'd like to use: "))
return list(printers.keys())[selected_printer - 1]
def get_product_code():
product_code = input("Enter the product code: ")
return product_code.upper()
def generate_barcode(product_code):
barcode_image = treepoem.generate_barcode(barcode_type='code128', data=product_code)
output = BytesIO()
barcode_image.save(output, format='PNG')
return output.getvalue()
def print_barcode(printer, barcode_image):
conn = cups.Connection()
temp_filename = 'temp_barcode.png'
with open(temp_filename, 'wb') as f:
f.write(barcode_image)
options = {
'media': 'Custom.1x2.125in', # Set the media size to 1" x 2-1/8"
'fit-to-page': '', # Fit the barcode to the page
}
conn.printFile(printer, temp_filename, 'Barcode Print', options)
os.remove(temp_filename)
def main():
printers = list_printers()
selected_printer = user_select_printer(printers)
while True:
product_code = get_product_code()
barcode_image = generate_barcode(product_code)
print_barcode(selected_printer, barcode_image)
print("Barcode printed successfully.")
continue_printing = input("Do you want to print another barcode? (y/n): ")
if continue_printing.lower() != 'y':
break
if __name__ == "__main__":
main()
Editor is loading...