import treepoem
from io import BytesIO
import os
import platform
def list_printers():
if platform.system() == 'Windows':
os.system('wmic printer get name')
else:
os.system('lpstat -p -d')
def user_select_printer():
selected_printer = input("Enter the name of the printer you'd like to use: ")
return selected_printer
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):
temp_filename = 'temp_barcode.png'
with open(temp_filename, 'wb') as f:
f.write(barcode_image)
if platform.system() == 'Windows':
os.system(f'print /D:"{printer}" {temp_filename}')
else:
os.system(f'lp -d {printer} -o media=Custom.1x2.125in -o fit-to-page {temp_filename}')
os.remove(temp_filename)
def main():
list_printers()
selected_printer = user_select_printer()
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()