基于Python編寫一個打印機批量打印隊列工具
1、背景介紹
有時候我們在批量打印文件的時候(包括word文檔、PPT、Excel、圖片),總會遇到電腦上打印機隊列打不開的情況,為此我們可以利用Python寫一個打印機批量打印隊列!
將想要打印的文件全部拖入其中,然后就可以批量依次打?。?/p>
2、庫的安裝
| 庫 | 用途 | 安裝 |
|---|---|---|
| PyQt5 | 界面設計 | pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
| os | 獲取絕對路徑 | 內(nèi)置庫無需安裝 |
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文件,可以使用系統(tǒng)的命令來打印
# 對于圖片文件,可以通過Pillow庫來打印,或者使用操作系統(tǒng)的圖片查看器打印
if file_path.lower().endswith('.pdf'):
# 例如,使用Windows上的os.startfile命令來打印PDF文件
os.startfile(file_path, 'print')
elif file_path.lower().endswith('.docx'):
# 對于Word文件,可能需要通過外部工具來打?。ū热鏜icrosoft Word或LibreOffice命令行工具)
QMessageBox.information(self, '打印', f'正在打印Word文件: {file_path}')
elif file_path.lower().endswith(('.jpg', '.png', '.bmp')):
# 對于圖片文件,使用系統(tǒng)默認圖片查看器打印
os.startfile(file_path, 'print')
elif file_path.lower().endswith('.txt'):
# 對于文本文件,可以將內(nèi)容轉(zhuǎn)成打印的格式
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_())
效果圖

到此這篇關(guān)于基于Python編寫一個打印機批量打印隊列工具的文章就介紹到這了,更多相關(guān)Python批量打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文教會你利用Python程序讀取Excel創(chuàng)建折線圖
不同類型的圖表有不同的功能,柱形圖主要用于對比數(shù)據(jù),折線圖主要用于展示數(shù)據(jù)變化的趨勢,散點圖主要用于判斷數(shù)據(jù)的相關(guān)性,下面這篇文章主要給大家介紹了關(guān)于如何通過一文教你利用Python程序讀取Excel創(chuàng)建折線圖的相關(guān)資料,需要的朋友可以參考下2022-11-11
Python執(zhí)行遺傳編程gplearn庫使用實例探究
這篇文章主要為大家介紹了Python執(zhí)行遺傳編程gplearn庫使用實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Pytorch使用MNIST數(shù)據(jù)集實現(xiàn)CGAN和生成指定的數(shù)字方式
今天小編就為大家分享一篇Pytorch使用MNIST數(shù)據(jù)集實現(xiàn)CGAN和生成指定的數(shù)字方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Python使用 Beanstalkd 做異步任務處理的方法
這篇文章主要介紹了Python使用 Beanstalkd 做異步任務處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

