import barcode
from barcode.writer import ImageWriter
from PIL import Image,ImageWin
import win32print
import win32ui
options = {
'module_width': 0.24,
'module_height':6.0,
'font_size': 8,
'text_distance': 3,
'quiet_zone':4,
}
def print_img(image_path):
mr_id=0
has_750=False
done=False
printers = [printer[2] for printer in win32print.EnumPrinters(2)]
for i, printer in enumerate(printers):
# print(f"{i+1}: {printer}")
if '750' in printer:
mr_id=i
has_750=True
printer_name=printers[i]
if has_750:
print("已经提交给打印机",printer_name,"打印条码")
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
done=True
return done
def makeCode(code):
Code128 = barcode.get_barcode_class('code128')
code128 = Code128(code, writer=ImageWriter())
filename = code128.save('print_swap',options=options)
image_path=filename
print_img(image_path)
code='HTA101CWGL0120260001'
makeCode(code)
打印机 python使用 Pillow pywin32
发表评论