#!/usr/bin/env python
#-*- coding:utf-8 -*-
# bug
import pythoncom
import win32
#
# mqtt引用
import json5
import paho.mqtt.client as mqtt
import json
# mqtt引用
# 打印机引用
import os
from win32com.client import DispatchEx
import time
from queue import Queue
# 打印机引用
import os, sys
from tkinter import *
from tkinter.font import Font
from tkinter.ttk import *
#Usage:showinfo/warning/error,askquestion/okcancel/yesno/retrycancel
from tkinter.messagebox import *
#Usage:f=tkFileDialog.askopenfilename(initialdir='E:/Python')
#import tkinter.filedialog as tkFileDialog
#import tkinter.simpledialog as tkSimpleDialog #askstring()
class Application_ui(Frame):
#这个类仅实现界面生成功能,具体事件处理代码在子类Application中。
def __init__(self, master=None):
Frame.__init__(self, master)
self.master.title('新强云打印监听1.0')
self.master.geometry('304x201')
self.createWidgets()
def createWidgets(self):
self.top = self.winfo_toplevel()
self.style = Style()
self.style.configure('TLabel1.TLabel', anchor='w', font=('宋体',9))
self.Label1 = Label(self.top, text='该项目用于接收mqtt服务器数据执行打印机操作', style='TLabel1.TLabel')
self.Label1.place(relx=0.026, rely=0.08, relwidth=0.924, relheight=0.085)
self.style.configure('TCommand1.TButton', font=('宋体', 9))
self.Command1 = Button(self.top, text='启动', command=self.Command1_Cmd, style='TCommand1.TButton')
self.Command1.place(relx=0.684, rely=0.796, relwidth=0.293, relheight=0.164)
self.txt = Text(self.top, height=10, width=50)
self.txt.place(relx=0.026, rely=0.199, relwidth=0.951, relheight=0.443)
self.style.configure('TLabel2.TLabel', anchor='w', font=('新宋体', 9))
self.Label2 = Label(self.top, text='mqtt服务器状态:未连接', style='TLabel2.TLabel')
self.Label2.place(relx=0.053, rely=0.716, relwidth=0.503, relheight=0.085)
self.Label2.bind('<Button-1>', self.Label2_Button_1)
class Application(Application_ui):
#这个类实现具体的事件处理回调函数。界面生成代码在Application_ui中。
def __init__(self, master=None):
Application_ui.__init__(self, master)
def Label2_Button_1(self, event):
# TODO, Please finish the function here!
pass
def Command1_Cmd(self, event=None):
# mqtt监听
# 连接成功回调
def on_connect(client, userdata, flags, rc):
print('=====Connected with result code ' + str(rc) + "=====")
if str(rc)=='0':
self.Label2['text']='mqtt服务器状态:已连接'
client.subscribe('print_sub23421', qos=2)
# 接受订阅消息print_sub23421
def to_json(code, messg, data_):
json_data = {'code': 200, 'msg': messg, 'data': data_}
return json.dumps(json_data)
# 消息接收回调
def on_message(client, userdata, msg):
# data_=msg.payload.decode('UTF-8')
data = str(msg.payload.decode('UTF-8'))
jsonDict = json.loads(data)
msg=jsonDict['msg']
print(jsonDict['code'])
print(type(jsonDict))
self.txt.insert(END, msg + '\n')
if(jsonDict['code']==200):
self.txt.insert(END, "准备处理打印文件" + '\n')
q = Queue(600)
# 方法 遍历文件夹 将该文件夹下包括所有子文件夹下所有文档路径存入列表
def traver(path):
pathlist = os.listdir(path)
for file in pathlist:
abspath = os.path.join(path, file)
if os.path.isfile(abspath):
# 将文件名放入队列里
q.put(abspath)
else:
traver(abspath)
# break
# 通过路径 打印文档 队列q是全局变量 这里不用传参数
def docx_print():
while True:
if q.empty():
break
else:
# 取出一个
f = q.get()
# 通过对路径字符串切片后,取到扩展名,用于判断是表格还是文档
eName = f.split('.')[-1]
if eName == 'xls' or eName == 'xlsx':
# w = DispatchEx('excel.Application')
w = DispatchEx('excel.Application')
w.Visible = 0 # 不打开软件
w.DisplayAlerts = 0 # 不报错
doc = w.Workbooks.Open(f).Worksheets('Sheet1')
elif eName == 'doc' or eName == 'docx':
w = DispatchEx('kwps.Application')
w.Visible = 0 # 不打开软件
w.DisplayAlerts = 0 # 不报错
doc = w.Documents.Open(f)
print("正在打印:{}".format(f))
doc.PrintOut()
time.sleep(1)
w.Quit()
# 线程初始化
pythoncom.CoInitialize()
## 需要打印文件或文件夹的路径
path = r'C:\Users\MAC\Desktop\test'
traver(path)
print("文件读取完毕,共有{}个".format(q.qsize()))
docx_print()
# 释放资源
pythoncom.CoUninitialize()
client = mqtt.Client()
# 指定回调函数
client.on_connect = on_connect
client.on_message = on_message
# 建立连接
client.connect('broker.emqx.io', 1883, 60)
# 发布消息
test = to_json(200, "新强云监听工具连接成功", "")
self.txt.insert(END, test+'\n')
# 状态码,返回消息,返回数据
client.publish('print_pub23421', payload=test, qos=2)
client.loop_start()
self.Command1["text"]="监听中"
# mqtt监听
# q = Queue(600)
#
# # 方法 遍历文件夹 将该文件夹下包括所有子文件夹下所有文档路径存入列表
# def traver(path):
# pathlist = os.listdir(path)
# for file in pathlist:
# abspath = os.path.join(path, file)
# if os.path.isfile(abspath):
# # 将文件名放入队列里
# q.put(abspath)
# else:
# traver(abspath)
# # break
#
# # 通过路径 打印文档 队列q是全局变量 这里不用传参数
# def docx_print():
# while True:
# if q.empty():
# break
# else:
# # 取出一个
# f = q.get()
# # 通过对路径字符串切片后,取到扩展名,用于判断是表格还是文档
# eName = f.split('.')[-1]
#
# if eName == 'xls' or eName == 'xlsx':
# # w = DispatchEx('excel.Application')
# w = DispatchEx('exc
评论0