亲宝软件园·资讯

展开

Python打印文件 Python调用Windows命令打印文件

人气:0

1、Python调用Windows命令打印文件

Windows命令行打印文件使用print 命令,具体用法可使用help print查看。下面是使用Python调用print指令执行打印文件功能的代码:

import os
 
def printer(filename):
  printername = r"/D:\\A340\hpLaser"
  cmd = "print " + printername + " " + filename
  print "print cmd:", cmd
  if not os.system(cmd):
    print "printing..."
  else:
    print "some error occurs."
if __name__ == "__main__":
  filename = raw_input("Please input your filename:\n")
  printer(filename)

这里的打印机用的是共享打印机,所以需要指定域名和打印机名称,而且"/D:"表示的是打印设备的意思。

2、windows下如何用python控制打印机打印

参考网站

首先下载python需要的库

pip install pypiwin32

简单例子

import tempfile
import win32api
import win32print

filename = tempfile.mktemp (".txt")
open (filename, "w").write ("This is a test")
win32api.ShellExecute (
 0,
 "print",
 filename,
 #
 # If this is None, the default printer will
 # be used anyway.
 #
 '/d:"%s"' % win32print.GetDefaultPrinter (),
 ".",
 0
)
import win32print
import win32ui
import win32con

def print2Printer():
    INCH = 1440

    hDC = win32ui.CreateDC ()
    hDC.CreatePrinterDC (win32print.GetDefaultPrinter ())
    hDC.StartDoc ("Test doc")
    hDC.StartPage ()
    hDC.SetMapMode (win32con.MM_TWIPS)
    hDC.DrawText ("TEST HELLO WORLD! CORSS FIREWALL, WE TOUCH THE WORLD!",
            (0, INCH * -1, INCH * 8, INCH * -2), win32con.DT_CENTER)
    hDC.EndPage ()
    hDC.EndDoc ()

print2Printer()

若在hDC.DrawText()中传的是空字符串,则可以实现空走纸。

但实际上这只是打印自己制定的字符串, 并且打印出来会显示txt的文件名。所以我更倾向于使用windows的cmd指令打印指定文件。

3、Python利用win32连接打印机自动打印

import win32print
import win32ui
from PIL import Image, 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) for i in bmp.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、python chrome调用打印的样例

python selenium 控制chrome 调用打印的时候会找不到元素,控制不到的情况

# 点击打印
wait.until(ec.presence_of_element_located((By.LINK_TEXT, "打印"))).click()
# 父窗口 类名: Chrome_WidgetWin_1 标题: 中行网银(香港) - Google Chrome
father = win32gui.FindWindow(None, '中行网银(香港) - Google Chrome')
# 子窗口 类名: Chrome_RenderWidgetHostHWND 标题: Chrome Legacy Window
son = win32gui.FindWindowEx(father, None, "Chrome_RenderWidgetHostHWND", 'Chrome Legacy Window')
# 设置为最前窗口
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("print end")

另一种方法,调用pdf打印,代码如下:

import win32api
import os
import json

#PRINTER_NAME = '\\\\cnsz02vw00397\\CM_Printer_1'
#PRINTER_NAME = 'Xerox Phaser 3435 PCL 6'

def print_pdf(pdf_file_name):

with open(os.getcwd() + "\\config_generate.json", "r", encoding="utf-8") as f:
  json_data = json.load(f)
  PRINTER_MACHINE = json_data["print_machine"]
  f.close()
PRINTER_NAME = PRINTER_MACHINE
"""
静默打印pdf
:param pdf_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)

这里就差不多了

您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论