赞
踩
操作系统:win10
python:3.8.16
pip install openpyxl qrcode pillow pypiwin32 reportlab
python printer.py
运行后,会检索本机已经连接的打印机设备,然后通过需要选择打印机,然后传入图片路径即可下发打印任务到打印机(期间会对图片转矢量图)。
可以对图片输入部分做个批量自动化,就可以批量打印了。
import win32print import win32ui from PIL import Image, ImageWin # 列出所有打印机 printers = [printer[2] for printer in win32print.EnumPrinters(2)] for i, printer in enumerate(printers): print(f"{i+1}: {printer}") # 选择打印机 choice = int(input("选择要使用的打印机 (输入对应的序号): ")) - 1 printer_name = printers[choice] # 加载图片 image_path = input("输入要打印的图片的路径: ") image = Image.open(image_path) # 创建设备描述表 hDC = win32ui.CreateDC() hDC.CreatePrinterDC(printer_name) # 开始文档 hDC.StartDoc(image_path) # 开始页面 hDC.StartPage() # 绘制位图 dib = ImageWin.Dib(image) dib.draw(hDC.GetHandleOutput(), (0, 0, image.width, image.height)) # 结束页面 hDC.EndPage() # 结束文档 hDC.EndDoc() # 删除设备描述表对象 del hDC print("打印成功!")
会运行edge让你手动打印0.0
import subprocess import win32print from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import letter, landscape from PIL import Image # 列出所有打印机 printers = [printer[2] for printer in win32print.EnumPrinters(2)] for i, printer in enumerate(printers): print(f"{i+1}: {printer}") # 选择打印机 choice = int(input("选择要使用的打印机 (输入对应的序号): ")) - 1 printer_name = printers[choice] # 加载图片 image_path = input("输入要打印的图片的路径: ") image = Image.open(image_path) # 转换图片为PDF pdf_path = image_path.rsplit('.', 1)[0] + '.pdf' c = canvas.Canvas(pdf_path, pagesize=landscape(letter)) width, height = landscape(letter) c.drawImage(image_path, 0, 0, width, height) c.showPage() c.save() # 打印PDF edge_path = r'C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe' # 默认的Edge路径 cmd = [edge_path, '--kiosk-printing', pdf_path] subprocess.run(cmd) print("打印成功!")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。