Python调用Windows命令打印文件
1、Python调用Windows命令打印文件
Windows命令行打印文件使用print命令,具体用法可使用helpprint查看。下面是使用Python调用print指令执行打印文件功能的代码:
importos
defprinter(filename):
printername=r"/D:\\A340\hpLaser"
cmd="print"+printername+""+filename
print"printcmd:",cmd
ifnotos.system(cmd):
print"printing..."
else:
print"someerroroccurs."
if__name__=="__main__":
filename=raw_input("Pleaseinputyourfilename:\n")
printer(filename)
这里的打印机用的是共享打印机,所以需要指定域名和打印机名称,而且"/D:"表示的是打印设备的意思。
2、windows下如何用python控制打印机打印
参考网站
首先下载python需要的库
pipinstallpypiwin32
简单例子
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
)
importwin32print
importwin32ui
importwin32con
defprint2Printer():
INCH=1440
hDC=win32ui.CreateDC()
hDC.CreatePrinterDC(win32print.GetDefaultPrinter())
hDC.StartDoc("Testdoc")
hDC.StartPage()
hDC.SetMapMode(win32con.MM_TWIPS)
hDC.DrawText("TESTHELLOWORLD!CORSSFIREWALL,WETOUCHTHEWORLD!",
(0,INCH*-1,INCH*8,INCH*-2),win32con.DT_CENTER)
hDC.EndPage()
hDC.EndDoc()
print2Printer()
若在hDC.DrawText()中传的是空字符串,则可以实现空走纸。
但实际上这只是打印自己制定的字符串,并且打印出来会显示txt的文件名。所以我更倾向于使用windows的cmd指令打印指定文件。
3、Python利用win32连接打印机自动打印
importwin32print importwin32ui fromPILimportImage,ImageWin #物理宽度、高度 PHYSICALWIDTH=110 PHYSICALHEIGHT=111 #物理偏移位置 PHYSICALOFFSETX=112 PHYSICALOFFSETY=113 printer_name=win32print.GetDefaultPrinter() #选择图片路径 file_name=“biaoqian.png” hDC=win32ui.CreateDC() hDC.CreatePrinterDC(printer_name) printer_size=hDC.GetDeviceCaps(PHYSICALWIDTH),hDC.GetDeviceCaps(PHYSICALHEIGHT) #printer_margins=hDC.GetDeviceCaps(PHYSICALOFFSETX),hDC.GetDeviceCaps(PHYSICALOFFSETY) #打开图片 bmp=Image.open(file_name) print(bmp.size) ratios=[1.0*1754/bmp.size[0],1.0*1240/bmp.size[1]] scale=min(ratios) print(ratios) print(scale) hDC.StartDoc(file_name) hDC.StartPage() dib=ImageWin.Dib(bmp) scaled_width,scaled_height=[int(scale*i)foriinbmp.size] print(scaled_width,scaled_height) x1=int((printer_size[0]-scaled_width)/2) y1=int((printer_size[1]-scaled_height)/2) #横向位置坐标 x1=1580 #竖向位置坐标 y1=30 #4倍为自适应图片实际尺寸打印 x2=x1+bmp.size[0]*4 y2=y1+bmp.size[1]*4 dib.draw(hDC.GetHandleOutput(),(x1,y1,x2,y2)) hDC.EndPage() hDC.EndDoc() hDC.DeleteDC()
4、pythonchrome调用打印的样例
pythonselenium控制chrome调用打印的时候会找不到元素,控制不到的情况
#点击打印
wait.until(ec.presence_of_element_located((By.LINK_TEXT,"打印"))).click()
#父窗口类名:Chrome_WidgetWin_1标题:中行网银(香港)-GoogleChrome
father=win32gui.FindWindow(None,'中行网银(香港)-GoogleChrome')
#子窗口类名:Chrome_RenderWidgetHostHWND标题:ChromeLegacyWindow
son=win32gui.FindWindowEx(father,None,"Chrome_RenderWidgetHostHWND",'ChromeLegacyWindow')
#设置为最前窗口
win32gui.SetForegroundWindow(son)
time.sleep(1)
#ctrl+P
win32api.keybd_event(17,0,0,0)
win32api.keybd_event(80,0,0,0)
win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(80,0,win32con.KEYEVENTF_KEYUP,0)
time.sleep(1)
#ctrl+shift+P
win32api.keybd_event(17,0,0,0)
win32api.keybd_event(16,0,0,0)
win32api.keybd_event(80,0,0,0)
win32api.keybd_event(17,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(16,0,win32con.KEYEVENTF_KEYUP,0)
win32api.keybd_event(80,0,win32con.KEYEVENTF_KEYUP,0)
time.sleep(1)
#发送回车-确认打印
win32api.keybd_event(13,0,0,0)
win32api.keybd_event(13,0,win32con.KEYEVENTF_KEYUP,0)
time.sleep(1)
#关闭子窗口
windows=browser.window_handles
browser.switch_to.window(windows[1])
wait.until(ec.presence_of_element_located((By.NAME,"close"))).click()
#切回父窗口
browser.switch_to.window(windows[0])
print(browser.page_source)
#点击返回
wait.until(ec.presence_of_element_located((By.LINK_TEXT,"返回"))).click()
print("printend")
另一种方法,调用pdf打印,代码如下:
importwin32api importos importjson #PRINTER_NAME='\\\\cnsz02vw00397\\CM_Printer_1' #PRINTER_NAME='XeroxPhaser3435PCL6' defprint_pdf(pdf_file_name): withopen(os.getcwd()+"\\config_generate.json","r",encoding="utf-8")asf: json_data=json.load(f) PRINTER_MACHINE=json_data["print_machine"] f.close() PRINTER_NAME=PRINTER_MACHINE """ 静默打印pdf :parampdf_file_name: :return: """ GHOSTSCRIPT_PATH=os.getcwd()+'\\GHOSTSCRIPT\\bin\\gswin32c' print(GHOSTSCRIPT_PATH) #currentprinter=config.printerName currentprinter=PRINTER_NAME #currentprinter=win32print.GetDefaultPrinter() arg='-dPrinted'\ '-dBATCH'\ '-dNOPAUSE'\ '-dNOSAFER'\ '-dFitPage'\ '-dNORANGEPAGESIZE'\ '-q'\ '-dNumCopies=1'\ '-sDEVICE=mswinpr2'\ '-sOutputFile="\\\\spool\\'\ +currentprinter+"\""+""+\ pdf_file_name #log.info(arg) win32api.ShellExecute( 0,\ 'open',\ GHOSTSCRIPT_PATH,\ arg,\ ".", 0 ) if__name__=='__main__': aaa='D:\\tempFile\\2019-09-12\\pay\\print.pdf' print_pdf(aaa)
这里就差不多了