Python调用打印机参考例子
参考资料:
http://blog.csdn.net/jdh99/article/details/42585987
http://www.oschina.net/question/1438043_235020
http://blog.163.com/ctcg326%40126/blog/static/758129102012451034427/
http://codego.net/447659/
部分样例:
1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | from PyQt5.QtWidgets import (QApplication, QWidget, QTableWidget,QPushButton, QVBoxLayout, QTableWidgetItem) from PyQt5.QtGui import QPixmap, QPainter, QImage, QTextDocument from PyQt5.QtPrintSupport import QPrinter, QPrintDialog, QPrintPreviewDialog from PyQt5.QtCore import QRect, QPoint, QSize , Qt # 有预览框<br># 908204694@qq.com<br># def on_htmlButton_clicked(): printer = QPrinter(QPrinter.HighResolution) #/* 打印预览 */ preview = QPrintPreviewDialog(printer,widget ) preview.paintRequested.connect(printHtml) # # 显示 预览框 # #preview.exec() preview.exec_() def printHtml(printer): html = """<html> <head></head> <body> <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b><h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> <h1>55555</h1><b>bold</b> </body> </html>""" textDocument = QTextDocument() textDocument.setHtml(html) #textDocument.print(printer) textDocument.print_(printer) def on_picButton_clicked(): printer = QPrinter(QPrinter.HighResolution) #/* 打印预览 */ preview = QPrintPreviewDialog(printer,widget ) """ * QPrintPreviewDialog类提供了一个打印预览对话框,里面功能比较全, * paintRequested(QPrinter *printer)是系统提供的, * 当preview.exec()执行时该信号被触发, * plotPic(QPrinter *printer)是用户自定义的槽函数,图像的绘制就在这个函数里。 """ preview.paintRequested.connect(plotPic) preview. exec () #/* 等待预览界面退出 */ def plotPic(printer): painter = QPainter(printer); image = QPixmap() image = widget.grab(QRect( QPoint( 0 , 0 ), QSize( widget.size().width(), widget.size().height() ) ) ) # /* 绘制窗口至画布 */ #QRect rect = painter.viewport(); #QSize size = image.size(); size.scale(rect.size(), Qt.KeepAspectRatio) # //此处保证图片显示完整 painter.setViewport(rect.x(), rect.y(),size.width(), size.height()); painter.setWindow(image.rect()); painter.drawPixmap( 0 , 0 ,image); #/* 数据显示至预览界面 */ import sys app = QApplication(sys.argv) tablewidget = QTableWidget() ## 设置列数 tablewidget.setColumnCount( 4 ) tablewidget.horizontalHeader().setDefaultSectionSize( 150 ) ## QStringList在PyQt5 header = [ "name" , "last modify time" , "type" , "size" ] tablewidget.setHorizontalHeaderLabels(header) tablewidget.insertRow( 0 ) tablewidget.insertRow( 0 ) pItem1 = QTableWidgetItem( "aa" ) pItem2 = QTableWidgetItem( "bb" ) pItem3 = QTableWidgetItem( "cc" ) pItem4 = QTableWidgetItem( "dd" ) tablewidget.setItem( 0 , 0 , pItem1 ) tablewidget.setItem( 0 , 1 , pItem2 ) tablewidget.setItem( 0 , 2 , pItem3 ) tablewidget.setItem( 0 , 3 , pItem4 ) tablewidget.setMinimumSize( 800 , 600 ) button = QPushButton( '打印界面' ) button.clicked.connect(on_picButton_clicked) button_txt = QPushButton( '打印文字' ) button_txt.clicked.connect(on_htmlButton_clicked) widget = QWidget() layout = QVBoxLayout(widget) layout.addWidget(button) layout.addWidget(button_txt) layout.addWidget(tablewidget) widget.show() sys.exit(app.exec_()) |
2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #------------------------- printer.py ---------------------- # 908204694@qq.com # 直接打印,不预览 # -*- coding: utf-8 -*- from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtPrintSupport import QPrinterInfo, QPrinter class Printer: #打印机列表 @staticmethod def printerList(): printer = [] printerInfo = QPrinterInfo() print ( 'availablePrinterNames' , printerInfo.availablePrinterNames() ) print ( 'defaultPrinterName' , printerInfo.defaultPrinterName()) for item in printerInfo.availablePrinters(): printer.append(item.printerName()) return printer #打印任务 @staticmethod def printing(printer, context): p = QPrinter() doc = QTextDocument() htmlStr = context print ( 'aaaa' , htmlStr) doc.setHtml(htmlStr) doc.setPageSize(QSizeF(p.logicalDpiX() * ( 80 / 25.4 ), p.logicalDpiY() * ( 297 / 25.4 ))) p.setOutputFormat(QPrinter.NativeFormat) doc.print_(p) @staticmethod def printing_22(printer, context): printerInfo = QPrinterInfo() p = QPrinter() for item in printerInfo.availablePrinters(): if printer = = item.printerName(): p = QPrinter(item) doc = QTextDocument() doc.setHtml(u '%s' % context) doc.setPageSize(QSizeF(p.logicalDpiX() * ( 80 / 25.4 ), p.logicalDpiY() * ( 297 / 25.4 ))) p.setOutputFormat(QPrinter.NativeFormat) doc.print_(p) if __name__ = = '__main__' : import sys from PyQt5.QtWidgets import QApplication app = QApplication(sys.argv) ########################################## html = '<html><head></head><body><h1>55555</h1><b>bold</b></body></html>' p = "defaultPrinter" #打印机名称 #Printer.printing(p, html) #Printer.printerList() Printer.printing_22(p, html) ##################################################### sys.exit(app.exec_()) |
3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import QApplication from PyQt5.QtPrintSupport import QPrinter # # 将要打印的东西生成pdf # # 908204694@qq.com a = QApplication([]) document = QTextDocument() html = """ <head> <title>Report</title> <style> </style> </head> <body> <table width="100%"> <tr> <td><img src="{}" width="30"></td> <td><h1>REPORT汉字试试哈</h1></td> </tr> </table> <hr> <p align=right><img src="{}" width="300"></p> <p align=right>Sample</p> </body> """ . format ( './aa.png' , './bb.png' ) document.setHtml(html) printer = QPrinter() printer.setResolution( 96 ) printer.setPageSize(QPrinter.Letter) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName( "test.pdf" ) # 设置纸张的边距 printer.setPageMargins( 12 , 16 , 12 , 20 , QPrinter.Millimeter) document.setPageSize(QSizeF(printer.pageRect().size())) print (document.pageSize(), printer.resolution(), printer.pageRect()) document.print_(printer) |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步