【python】云打印实现

这两天为了实现云打印功能找了很多相关的文章

记录一下这一篇,python云打印实现-朝花夕拾,代码通过监听文件夹有无产生新文件来判断是否执行,我尝试运行了下没问题,于是打算转载一下

程序运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

由于对方的代码和我实现的有点出入但都是先下载,后打印,只不过我的实通过mqtt服务器罢了,于是通过一步一步解析,我发现他这里有几个需要注意的,给大家和自己才个坑

1、settings.json 配置打印机和监控文件夹 2、不支持图片和pdf打印,有点难弄,后面处理了我会附上 3、不支持彩彩印规格这些 4、打印机用默认的即可,配置没多大作用,因为最后参数没有传过去

我改良后的

代码语言:javascript
复制
import time

import win32api
import win32print
from watchdog.observers import Observer
from watchdog.events import *
import json
import sys
import traceback
import os

def printer_loading(filename, printer):
win32api.ShellExecute(
0,
"print",
filename,
'/d:"%s"' % win32print.GetDefaultPrinter(),
".",
0
)
'''
def printer_pic(pic_path, printer_name):
# 物理宽度、高度
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
# 物理偏移位置
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter()
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)
ratios = [1.0 * 1754 / bmp.size[0], 1.0 * 1240 / bmp.size[1]]
scale = min(ratios)
# #开始打印作业,并将位图绘制到
# #按比例缩放打印机设备。
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()

'''
class FileEventHandler(FileSystemEventHandler):
def init(self,printer):
FileSystemEventHandler.init(self)
self.printer = printer

def on_created(self, event):
    print(event.is_directory)
    if event.is_directory:
        print("directory created:{0}".format(event.src_path))
    else:
        print("file created:{0}".format(event.src_path))
        if event.src_path.endswith("docx") or event.src_path.endswith("doc") or event.src_path.endswith("xlsx") or event.src_path.endswith("jpg") or event.src_path.endswith("pdf") or event.src_path.endswith("xls") or event.src_path.endswith("txt") or event.src_path.endswith("img"):
            if "~" not in event.src_path:
                print(f"打印路径{event.src_path}")
                print(f"打印机名字{self.printer}")
                printer_loading(event.src_path, self.printer)

import time

win32api.ShellExecute(0, 'open', 'C:\Program Files (x86)\Allway Sync\Bin\syncappw.exe', '','',1)

if name == "main":

try:
    observers = []
    print("当前检测到的打印机:")
    maxL = 2
    Num = 2
    for i in range(1, 10):
        # print(i)
        r = win32print.EnumPrinters(i)
        l = len(r)
        if l > maxL:
            maxL = l
            Num = i

    for i, p in enumerate(list(win32print.EnumPrinters(Num))):
        print(f"\t{i}:{p[1]}\n")

    # f = open('.ttings.txt', 'r', encoding="utf-8")
    settings = {}
    # print("当前配置:")
    # for line in f.readlines():
    #     k, v = line.split(",")
    #     settings[k.strip()] = v.strip()
    #     print(f"\t{k}:{v}")



    print("当前配置:")
    with open("./settings.json", 'rb') as load_f:
        ds = json.load(load_f)
        for d in ds:
            settings[d['url'].strip()] = d["printer"].strip()
            print(f"\t{d}")
    pass
    for path, printer in settings.items():
        observer = Observer()
        print("打印机名"+printer)
        event_handler = FileEventHandler(printer)
        observer.schedule(event_handler, path, True)
        observer.start()
        observers.append(observer)

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        for observer in observers:
            observer.stop()

    for observer in observers:
        observer.join()
except Exception as e:
    print(e)
    traceback.print_exc()
    input()</code></pre></div></div><p>完整代码在这里:xxxxx</p><figure class=""><hr/></figure><p>我顺藤摸瓜发现我们启动的方法不一样,我的是通过</p><div class="rno-markdown-code"><div class="rno-markdown-code-toolbar"><div class="rno-markdown-code-toolbar-info"><div class="rno-markdown-code-toolbar-item is-type"><span class="is-m-hidden">代码语言:</span>javascript</div></div><div class="rno-markdown-code-toolbar-opt"><div class="rno-markdown-code-toolbar-copy"><i class="icon-copy"></i><span class="is-m-hidden">复制</span></div></div></div><div class="developer-code-block"><pre class="prism-token token line-numbers language-javascript"><code class="language-javascript" style="margin-left:0"> w = DispatchEx(&#39;kwps.Application&#39;)

w.Visible = 0 # 不打开软件
w.DisplayAlerts = 0 # 不报错
doc = w.Documents.Open(f)

而他的核心代码是通过

代码语言:javascript
复制
 # 打印输出
def printer_loading(filename, printer):
win32api.ShellExecute(
0,
"print",
filename,
'/d:"%s"' % win32print.GetDefaultPrinter(),
".",
0
)
# 打印

暂时只知道第一个代码需要安装wps,第二个未测试

client_mqtt版(需要配合mqtt数据使用,不建议下载,自己用,需要安装wps-珠海zf版本)

client_mqtt_plus(需要配合mqtt数据使用,不建议下载,自己用,需要安装wps-珠海zf版本)

朝花夕拾-改良版(可以下载)