updated printer list to show local networ printers

This commit is contained in:
2026-02-06 10:48:19 +02:00
parent b9025fcabe
commit 1cf4482914
3 changed files with 29 additions and 31 deletions

View File

@@ -41,38 +41,21 @@ def get_available_printers():
return list(printers.keys()) if printers else ["PDF"]
elif SYSTEM == "Windows":
# Windows: Get both local and network printers
# Windows: Get local + connected printers (includes print server connections)
try:
printers = []
# Get local printers
# PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS captures:
# - Locally installed printers
# - Printers connected from a print server (e.g. \\server\printer)
try:
for printer_info in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL):
flags = win32print.PRINTER_ENUM_LOCAL | win32print.PRINTER_ENUM_CONNECTIONS
for printer_info in win32print.EnumPrinters(flags):
printer_name = printer_info[2]
if printer_name and printer_name not in printers:
printers.append(printer_name)
except:
pass
# Get network printers from print server
try:
for printer_info in win32print.EnumPrinters(win32print.PRINTER_ENUM_NETWORK):
printer_name = printer_info[2]
if printer_name and printer_name not in printers:
printers.append(printer_name)
except:
pass
# Get connected printers (alternative method using WMI)
try:
import wmi
c = wmi.WMI()
for printer in c.Win32_Printer():
printer_name = printer.Name
if printer_name and printer_name not in printers:
printers.append(printer_name)
except:
pass
except Exception as e:
print(f"Error enumerating printers: {e}")
# Add PDF as fallback option
if "PDF" not in printers:
@@ -266,22 +249,37 @@ def print_to_printer(printer_name, file_path):
return True
elif SYSTEM == "Windows":
# Windows: Use win32print or open with default printer
# Windows: Use win32print API for reliable printing (supports UNC paths)
try:
if WIN32_AVAILABLE:
import win32print
import win32api
# Print using the Windows API
win32api.ShellExecute(0, "print", file_path, f'/d:"{printer_name}"', ".", 0)
# Set the target printer as default temporarily, then print
# This approach works reliably with both local and UNC printer paths
try:
old_default = win32print.GetDefaultPrinter()
except:
old_default = None
try:
win32print.SetDefaultPrinter(printer_name)
win32api.ShellExecute(0, "print", file_path, None, ".", 0)
print(f"Label sent to printer: {printer_name}")
finally:
# Restore original default printer
if old_default:
try:
win32print.SetDefaultPrinter(old_default)
except:
pass
return True
else:
# Fallback: Open with default printer
if file_path.endswith('.pdf'):
os.startfile(file_path, "print")
else:
# For images, use default print application
subprocess.run([f'notepad', '/p', file_path], check=False)
subprocess.run(['notepad', '/p', file_path], check=False)
print(f"Label sent to default printer")
return True
except Exception as e: