python连接打印机实现打印文档、图片、pdf文件等功能
引言
python连接打印机进行打印,可能根据需求的不同,使用不同的函数模块。
- 如果你只是简单的想打印文档,比如office文档,你可以使用ShellExecute方法,对于微软office的文档、pdf、txt等有用,你可以尝试下;
- 如果你输入某些数据,文字信息,就想直接把它发送给打印机打印,那么可以尝试使用win32print;
- 如果你有一张图片,那么你可以结合python的PythonImagingLibrary(PIL)和win32ui模块进行打印;
普通打印
ShellExecute
- 首先确保你电脑中的应用可以打开你要打印的文件;
- 是一些标准的文件类型
- 不用管哪些打印机,也就是说和连接的打印机型号无关;
- 你无控制设置打印属性的权限;
importtempfile
importwin32api
importwin32print
filename=tempfile.mktemp(".txt")
open(filename,"w").write("Thisisatest")
win32api.ShellExecute(
0,
"print",
filename,
#
#IfthisisNone,thedefaultprinterwill
#beusedanyway.
#
'/d:"%s"'%win32print.GetDefaultPrinter(),
".",
0
)
另一个版本
importtempfile
importwin32api
importwin32print
filename=tempfile.mktemp(".txt")
open(filename,"w").write("Thisisatest")
win32api.ShellExecute(
0,
"printto",
filename,
'"%s"'%win32print.GetDefaultPrinter(),
".",
0
)
直接打印数据
win32print
- 直接将数据扔给打印机;
- 快速而且容易;
- 而且可以定义选择哪个打印机打印;
- 但是要打印的数据必须是可打印的,例如字符串等;
importos,sys
importwin32print
printer_name=win32print.GetDefaultPrinter()
#
#raw_datacouldequallyberawPCL/PSreadfrom
#someprint-to-fileoperation
#
ifsys.version_info>=(3,):
raw_data=bytes("Thisisatest","utf-8")
else:
raw_data="Thisisatest"
hPrinter=win32print.OpenPrinter(printer_name)
try:
hJob=win32print.StartDocPrinter(hPrinter,1,("testofrawdata",None,"RAW"))
try:
win32print.StartPagePrinter(hPrinter)
win32print.WritePrinter(hPrinter,raw_data)
win32print.EndPagePrinter(hPrinter)
finally:
win32print.EndDocPrinter(hPrinter)
finally:
win32print.ClosePrinter(hPrinter)
打印图片
PILwin32ui
不使用额外的工具,在windows电脑上打印一张图片是相当的困难,至少需要3种不同的且相关的设备环境才可以。
还好,device-independentbitmap(DIB)和PIL可以帮助我们快速打印。下面的代码可以将图片发送至打印机打印尽可能大的尺寸且不失比例。
- 还可以选择使用哪个打印机
- 选择加载的图片的格式等
- 但是如果你电脑不是windows,那可能不是最好的方法;
importwin32print importwin32ui fromPILimportImage,ImageWin # #ConstantsforGetDeviceCaps # # #HORZRES/VERTRES=printablearea # HORZRES=8 VERTRES=10 # #LOGPIXELS=dotsperinch # LOGPIXELSX=88 LOGPIXELSY=90 # #PHYSICALWIDTH/HEIGHT=totalarea # PHYSICALWIDTH=110 PHYSICALHEIGHT=111 # #PHYSICALOFFSETX/Y=left/topmargin # PHYSICALOFFSETX=112 PHYSICALOFFSETY=113 printer_name=win32print.GetDefaultPrinter() file_name="test.jpg" # #YoucanonlywriteaDevice-independentbitmap #directlytoaWindowsdevicecontext;therefore #weneed(forease)tousethePythonImaging #Librarytomanipulatetheimage. # #Createadevicecontextfromanamedprinter #andassesstheprintablesizeofthepaper. # hDC=win32ui.CreateDC() hDC.CreatePrinterDC(printer_name) printable_area=hDC.GetDeviceCaps(HORZRES),hDC.GetDeviceCaps(VERTRES) printer_size=hDC.GetDeviceCaps(PHYSICALWIDTH),hDC.GetDeviceCaps(PHYSICALHEIGHT) printer_margins=hDC.GetDeviceCaps(PHYSICALOFFSETX),hDC.GetDeviceCaps(PHYSICALOFFSETY) # #Opentheimage,rotateitifit'swiderthan #itishigh,andworkouthowmuchtomultiply #eachpixelbytogetitasbigaspossibleon #thepagewithoutdistorting. # bmp=Image.open(file_name) ifbmp.size[0]>bmp.size[1]: bmp=bmp.rotate(90) ratios=[1.0*printable_area[0]/bmp.size[0],1.0*printable_area[1]/bmp.size[1]] scale=min(ratios) # #Starttheprintjob,anddrawthebitmapto #theprinterdeviceatthescaledsize. # hDC.StartDoc(file_name) hDC.StartPage() dib=ImageWin.Dib(bmp) scaled_width,scaled_height=[int(scale*i)foriinbmp.size] x1=int((printer_size[0]-scaled_width)/2) y1=int((printer_size[1]-scaled_height)/2) x2=x1+scaled_width y2=y1+scaled_height dib.draw(hDC.GetHandleOutput(),(x1,y1,x2,y2)) hDC.EndPage() hDC.EndDoc() hDC.DeleteDC()
实践
从前台传来要打印的字符,后端生成二维码,并作出相应处理后,连接打印机打印图片。
#打印二维码
defprint_barcode(request):
importpyqrcode
importrandom,string
fromPILimportImage,ImageDraw,ImageFont
importnumpyasnp
ifrequest.is_ajax()andrequest.method=='POST':
result={}
bar_string='NaN'
type=request.POST['type']
iftype=='box':
#生成箱子码
#格式:P190823-K91[P][日期][-][A-Z][0-9][0-9]
bar_string='P'+datetime.date.today().strftime('%y%m%d')+'-'+str(random.choice('ABCDEFGHIGKLMNOPQRSTUVWXYZ'))\
+str(random.choice(range(10)))+str(random.choice(range(10)))
eliftype=='kuwei':
#生成库位码
bar_string=request.POST['string']
else:
pass
try:
big_code=pyqrcode.create(bar_string,error='L',version=2,mode='binary')
big_code.png('./code.png',scale=8)
img_code=Image.open('code.png')
size=img_code.size
img_final=Image.new('RGB',(size[0],size[1]+35),color=(255,255,255))
img_final.paste(img_code,(0,0,size[0],size[1]))
draw=ImageDraw.Draw(img_final)
font=ImageFont.truetype('AdobeGothicStd-Bold.otf',size=35)
width,height=draw.textsize(bar_string,font=font)
draw.text(((size[0]-width)/2,size[1]-15),bar_string,fill=(0,0,0),font=font)
img_final.save('./code.png')
#然后连接打印机将其打印出来即可
is_ok=[]
iftype=='box':
foriinrange(4):
temp=print_img('./code.png')
is_ok.append(temp)
else:
temp=print_img('./code.png')
is_ok.append(temp)
#is_ok=True
result['done']='ok'ifnp.all(is_ok)else'连接打印机失败'
exceptExceptionase:
result['done']=e
returnJsonResponse(result)
defprint_img(img):
importwin32print
importwin32ui
fromPILimportImage,ImageWin
#参考http://timgolden.me.uk/python/win32_how_do_i/print.html#win32print
try:
printer_name=win32print.GetDefaultPrinter()
hDC=win32ui.CreateDC()
hDC.CreatePrinterDC(printer_name)
#printable_area=(300,270)#打印纸尺寸
#printer_size=(300,270)
#打开图片并缩放
bmp=Image.open(img)
ifbmp.size[0]
打印效果:
以上内容为二赛君整理发布,转载请注明出处,谢谢。
参考
http://timgolden.me.uk/python/win32_how_do_i/print.htm
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。