Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.0 kB
2
Indexable
import barcode
from barcode.writer import ImageWriter
from PIL import Image, ImageWin, ImageOps
import os
import platform
import subprocess
import win32print
import win32ui
from ctypes import windll

def get_printers():
    printers = [printer[2] for printer in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)]
    return printers

def user_select_printer(printers):
    if not printers:
        print("No printers found.")
        return None

    print("List of printers:")
    for i, printer in enumerate(printers, start=1):
        print(f"{i}. {printer}")

    selected_index = int(input("Enter the number of the printer you'd like to use: "))
    return printers[selected_index - 1]

def get_product_code():
    product_code = input("Enter the product code: ")
    return product_code.upper()

def generate_barcode(product_code):
    Code128 = barcode.get_barcode_class('code128')
    writer = ImageWriter()
    writer.module_width = 100
    writer.module_height = 150
    writer.font_size = 12
    writer.text_distance = 0
    writer.quiet_zone = 6.5
    barcode_image = Code128(product_code, writer=writer)
    output_filename = 'temp_barcode'
    barcode_image.save(output_filename)
    return output_filename

def print_barcode(printer, barcode_image):
    # Get the printer DC
    printer_dc = win32ui.CreateDC()
    printer_dc.CreatePrinterDC(printer)
    # Get the page size and image size in pixels
    page_size_mm = (25.4, 54)
    dpi = 300  # Use a fixed DPI value (adjust it according to your printer's DPI)
    page_size_pixels = tuple(int(dpi * page_size_mm[i] / 25.4) for i in (0, 1))
    image = Image.open(barcode_image+".png")
    # Rotate the image by 90 degrees
    image = image.rotate(90, expand=True)
    image_width, image_height = image.size
    # Set the new size directly based on the paper dimensions
    new_size = (300, int(300 * 2.125))
    # Resize the image without maintaining the aspect ratio
    resized_image = image.resize(new_size, Image.LANCZOS)
    resized_width, resized_height = resized_image.size
    # Calculate the position to center the image on the page
    x_pos = int((page_size_pixels[0] - resized_width) / 2)
    y_pos = int((page_size_pixels[1] - resized_height) / 2) - 80
    # Start the print job
    printer_dc.StartDoc(barcode_image+".png")
    printer_dc.StartPage()
    # Print the image
    dib = ImageWin.Dib(resized_image)
    dib.draw(printer_dc.GetSafeHdc(), (x_pos, y_pos, resized_width, resized_height))
    # End the print job
    printer_dc.EndPage()
    printer_dc.EndDoc()
    printer_dc.DeleteDC()







def main():
    product_code = get_product_code()
    barcode_image = generate_barcode(product_code)
    printers = get_printers()
    selected_printer = user_select_printer(printers)

    if selected_printer is None:
        return

    while True:
        product_code = get_product_code()
        barcode_image = generate_barcode(product_code)
        print_barcode(selected_printer, barcode_image)

if __name__ == "__main__":
    main()