基于Python编写一个打印机批量打印队列工具
作者:一晌小贪欢
有时候我们在批量打印文件的时候,总会遇到电脑上打印机队列打不开的情况,为此我们可以利用Python写一个打印机批量打印队列,下面小编就来和大家详细讲讲吧
1、背景介绍
有时候我们在批量打印文件的时候(包括word文档、PPT、Excel、图片),总会遇到电脑上打印机队列打不开的情况,为此我们可以利用Python写一个打印机批量打印队列!
将想要打印的文件全部拖入其中,然后就可以批量依次打印!
2、库的安装
| 库 | 用途 | 安装 |
|---|---|---|
| PyQt5 | 界面设计 | pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
| os | 获取绝对路径 | 内置库无需安装 |
3、核心代码
def printFiles(self):
if self.listWidget.count() == 0:
QMessageBox.warning(self, '错误', '没有文件可打印!')
return
printer = QPrinter()
printDialog = QPrintDialog(printer, self)
if printDialog.exec_() == QPrintDialog.Accepted:
for i in range(self.listWidget.count()):
file_path = self.listWidget.item(i).text()
if os.path.exists(file_path):
if file_path.lower().endswith('.pdf'):
os.startfile(file_path, 'print')
elif file_path.lower().endswith('.docx'):
QMessageBox.information(self, '打印', f'正在打印Word文件: {file_path}')
elif file_path.lower().endswith(('.jpg', '.png', '.bmp')):
os.startfile(file_path, 'print')
elif file_path.lower().endswith('.txt'):
QMessageBox.information(self, '打印', f'正在打印文本文件: {file_path}')
elif file_path.lower().endswith('.xlsx'):
QMessageBox.information(self, '打印', f'正在打印Excel文件: {file_path}')
else:
QMessageBox.warning(self, '错误', f'不支持直接打印的文件类型: {file_path}')
else:
QMessageBox.warning(self, '错误', f'文件不存在: {file_path}')
4、完整代码
# -*- coding: UTF-8 -*-
'''
@Project :打印机队列工具
@File :测试.py
@IDE :PyCharm
@Author :一晌小贪欢(278865463@qq.com)
@Date :2025/2/6 10:24
'''
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QListWidget, QPushButton, QFileDialog, QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
import os
class FilePrinterApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('文件批量打印工具')
self.setGeometry(900, 500, 400, 300)
layout = QVBoxLayout()
self.listWidget = QListWidget()
self.listWidget.setAcceptDrops(True)
self.listWidget.setDragEnabled(True)
self.listWidget.setDropIndicatorShown(True)
self.listWidget.dragEnterEvent = self.dragEnterEvent
self.listWidget.dragMoveEvent = self.dragMoveEvent
self.listWidget.dropEvent = self.dropEvent
layout.addWidget(self.listWidget)
self.printButton = QPushButton('打印')
self.printButton.clicked.connect(self.printFiles)
layout.addWidget(self.printButton)
self.setLayout(layout)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls():
event.setDropAction(Qt.CopyAction)
event.accept()
for url in event.mimeData().urls():
file_path = url.toLocalFile()
# 支持更多文件类型
if file_path.lower().endswith(('.pdf', '.docx', '.txt', '.xlsx', '.jpg', '.png', '.bmp')):
self.listWidget.addItem(file_path)
else:
QMessageBox.warning(self, '错误', '不支持的文件类型!')
else:
event.ignore()
def printFiles(self):
if self.listWidget.count() == 0:
QMessageBox.warning(self, '错误', '没有文件可打印!')
return
printer = QPrinter()
printDialog = QPrintDialog(printer, self)
if printDialog.exec_() == QPrintDialog.Accepted:
for i in range(self.listWidget.count()):
file_path = self.listWidget.item(i).text()
if os.path.exists(file_path):
# 这里可以使用不同的方法来打印不同类型的文件
# 例如,对于PDF文件,可以使用系统的命令来打印
# 对于图片文件,可以通过Pillow库来打印,或者使用操作系统的图片查看器打印
if file_path.lower().endswith('.pdf'):
# 例如,使用Windows上的os.startfile命令来打印PDF文件
os.startfile(file_path, 'print')
elif file_path.lower().endswith('.docx'):
# 对于Word文件,可能需要通过外部工具来打印(比如Microsoft Word或LibreOffice命令行工具)
QMessageBox.information(self, '打印', f'正在打印Word文件: {file_path}')
elif file_path.lower().endswith(('.jpg', '.png', '.bmp')):
# 对于图片文件,使用系统默认图片查看器打印
os.startfile(file_path, 'print')
elif file_path.lower().endswith('.txt'):
# 对于文本文件,可以将内容转成打印的格式
QMessageBox.information(self, '打印', f'正在打印文本文件: {file_path}')
elif file_path.lower().endswith('.xlsx'):
# 对于Excel文件,可以通过Excel程序进行打印
QMessageBox.information(self, '打印', f'正在打印Excel文件: {file_path}')
else:
QMessageBox.warning(self, '错误', f'不支持直接打印的文件类型: {file_path}')
else:
QMessageBox.warning(self, '错误', f'文件不存在: {file_path}')
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = FilePrinterApp()
ex.show()
sys.exit(app.exec_())
效果图

到此这篇关于基于Python编写一个打印机批量打印队列工具的文章就介绍到这了,更多相关Python批量打印内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
