基于Python編寫一個(gè)打印機(jī)批量打印隊(duì)列工具
1、背景介紹
有時(shí)候我們?cè)谂看蛴∥募臅r(shí)候(包括word文檔、PPT、Excel、圖片),總會(huì)遇到電腦上打印機(jī)隊(duì)列打不開的情況,為此我們可以利用Python寫一個(gè)打印機(jī)批量打印隊(duì)列!
將想要打印的文件全部拖入其中,然后就可以批量依次打??!
2、庫(kù)的安裝
庫(kù) | 用途 | 安裝 |
---|---|---|
PyQt5 | 界面設(shè)計(jì) | pip install PyQt5 -i https://pypi.tuna.tsinghua.edu.cn/simple/ |
os | 獲取絕對(duì)路徑 | 內(nèi)置庫(kù)無(wú)需安裝 |
3、核心代碼
def printFiles(self): if self.listWidget.count() == 0: QMessageBox.warning(self, '錯(cuò)誤', '沒有文件可打?。?) 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, '錯(cuò)誤', f'不支持直接打印的文件類型: {file_path}') else: QMessageBox.warning(self, '錯(cuò)誤', f'文件不存在: {file_path}')
4、完整代碼
# -*- coding: UTF-8 -*- ''' @Project :打印機(jī)隊(duì)列工具 @File :測(cè)試.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, '錯(cuò)誤', '不支持的文件類型!') else: event.ignore() def printFiles(self): if self.listWidget.count() == 0: QMessageBox.warning(self, '錯(cuò)誤', '沒有文件可打??!') 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): # 這里可以使用不同的方法來(lái)打印不同類型的文件 # 例如,對(duì)于PDF文件,可以使用系統(tǒng)的命令來(lái)打印 # 對(duì)于圖片文件,可以通過Pillow庫(kù)來(lái)打印,或者使用操作系統(tǒng)的圖片查看器打印 if file_path.lower().endswith('.pdf'): # 例如,使用Windows上的os.startfile命令來(lái)打印PDF文件 os.startfile(file_path, 'print') elif file_path.lower().endswith('.docx'): # 對(duì)于Word文件,可能需要通過外部工具來(lái)打?。ū热鏜icrosoft Word或LibreOffice命令行工具) QMessageBox.information(self, '打印', f'正在打印Word文件: {file_path}') elif file_path.lower().endswith(('.jpg', '.png', '.bmp')): # 對(duì)于圖片文件,使用系統(tǒng)默認(rèn)圖片查看器打印 os.startfile(file_path, 'print') elif file_path.lower().endswith('.txt'): # 對(duì)于文本文件,可以將內(nèi)容轉(zhuǎn)成打印的格式 QMessageBox.information(self, '打印', f'正在打印文本文件: {file_path}') elif file_path.lower().endswith('.xlsx'): # 對(duì)于Excel文件,可以通過Excel程序進(jìn)行打印 QMessageBox.information(self, '打印', f'正在打印Excel文件: {file_path}') else: QMessageBox.warning(self, '錯(cuò)誤', f'不支持直接打印的文件類型: {file_path}') else: QMessageBox.warning(self, '錯(cuò)誤', f'文件不存在: {file_path}') if __name__ == '__main__': app = QApplication(sys.argv) ex = FilePrinterApp() ex.show() sys.exit(app.exec_())
效果圖
到此這篇關(guān)于基于Python編寫一個(gè)打印機(jī)批量打印隊(duì)列工具的文章就介紹到這了,更多相關(guān)Python批量打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文教會(huì)你利用Python程序讀取Excel創(chuàng)建折線圖
不同類型的圖表有不同的功能,柱形圖主要用于對(duì)比數(shù)據(jù),折線圖主要用于展示數(shù)據(jù)變化的趨勢(shì),散點(diǎn)圖主要用于判斷數(shù)據(jù)的相關(guān)性,下面這篇文章主要給大家介紹了關(guān)于如何通過一文教你利用Python程序讀取Excel創(chuàng)建折線圖的相關(guān)資料,需要的朋友可以參考下2022-11-11Python執(zhí)行遺傳編程gplearn庫(kù)使用實(shí)例探究
這篇文章主要為大家介紹了Python執(zhí)行遺傳編程gplearn庫(kù)使用實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01詳解使用Python+Pycaret進(jìn)行異常檢測(cè)
異常檢測(cè)提供了在數(shù)據(jù)中發(fā)現(xiàn)模式、偏差和異常的途徑,這些模式、偏差和異常不限于模型的標(biāo)準(zhǔn)行為。本文將用Python?Pycaret進(jìn)行異常檢測(cè),感興趣的可以了解一下2022-03-03pytorch 如何使用amp進(jìn)行混合精度訓(xùn)練
這篇文章主要介紹了pytorch 使用amp進(jìn)行混合精度訓(xùn)練的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05Pytorch使用MNIST數(shù)據(jù)集實(shí)現(xiàn)CGAN和生成指定的數(shù)字方式
今天小編就為大家分享一篇Pytorch使用MNIST數(shù)據(jù)集實(shí)現(xiàn)CGAN和生成指定的數(shù)字方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-01-01Python使用 Beanstalkd 做異步任務(wù)處理的方法
這篇文章主要介紹了Python使用 Beanstalkd 做異步任務(wù)處理的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-04-04