博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python调用打印机参考例子
阅读量:6826 次
发布时间:2019-06-26

本文共 5720 字,大约阅读时间需要 19 分钟。

 参考资料:

 

 

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.

from PyQt5.QtWidgets import (QApplication, QWidget, QTableWidget,QPushButton,                              QVBoxLayout,                             QTableWidgetItem)from PyQt5.QtGui import  QPixmap, QPainter, QImage, QTextDocumentfrom PyQt5.QtPrintSupport import  QPrinter, QPrintDialog, QPrintPreviewDialogfrom PyQt5.QtCore import QRect, QPoint, QSize ,  Qt# 有预览框 # 908204694@qq.com #def on_htmlButton_clicked():    printer =QPrinter(QPrinter.HighResolution)    #/* 打印预览 */    preview =QPrintPreviewDialog(printer,widget )       preview.paintRequested.connect(printHtml)        #    #   显示 预览框    #    #preview.exec()    preview.exec_()def printHtml(printer):    html = """                

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold

55555

bold """ 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 sysapp = QApplication(sys.argv)tablewidget = QTableWidget()## 设置列数tablewidget.setColumnCount(4)tablewidget.horizontalHeader().setDefaultSectionSize(150)## QStringList在PyQt5header= ["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.

#------------------------- printer.py ----------------------# 908204694@qq.com# 直接打印,不预览# -*- coding: utf-8 -*-from PyQt5.QtCore import *from PyQt5.QtGui import *from PyQt5.QtPrintSupport import QPrinterInfo, QPrinterclass Printer:#打印机列表@staticmethoddef printerList():printer = []printerInfo = QPrinterInfo()print('availablePrinterNames', printerInfo.availablePrinterNames() )print('defaultPrinterName', printerInfo.defaultPrinterName())for item in printerInfo.availablePrinters():printer.append(item.printerName())return printer#打印任务@staticmethoddef 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)@staticmethoddef 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 sysfrom PyQt5.QtWidgets import QApplicationapp = QApplication(sys.argv) ########################################## html = '

55555

bold'p = "defaultPrinter" #打印机名称#Printer.printing(p, html)#Printer.printerList()Printer.printing_22(p, html)##################################################### sys.exit(app.exec_())

  

 

 

3.

from PyQt5.QtGui import *from PyQt5.QtCore import *from PyQt5.QtWidgets import QApplicationfrom PyQt5.QtPrintSupport import QPrinter## 将要打印的东西生成pdf## 908204694@qq.coma=QApplication([])document = QTextDocument()html = """Report

REPORT汉字试试哈


Sample

""".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)

  

 

  

 

你可能感兴趣的文章
CsGL着色的三角形
查看>>
后端码农谈前端(CSS篇)第七课:定位与浮动
查看>>
springboot(十八):使用Spring Boot集成FastDFS
查看>>
何勉:第一性原理和精益敏捷的规模化实施
查看>>
处理 Oracle SQL in 超过1000 的解决方案
查看>>
AI领域三大范式的发展简史
查看>>
《云计算:原理与范式》一第2章 迁移到云2.1 引言
查看>>
《工业控制网络安全技术与实践》一一1.5 本章小结
查看>>
报道称土耳其屏蔽网盘和GitHub以防止邮件泄露
查看>>
美最大IDC服务商Equinix宣布将继续使用可再生能源
查看>>
大规模实时数据处理openPDC
查看>>
美司法部要求Facebook配合国税局查税
查看>>
Appium环境抢建(for web browser test)
查看>>
安防云计算核心技术探讨
查看>>
可从网站抓取数据的数据析取平台Import.io获1300万美元A轮融资
查看>>
混合云VPC组网场景和方案分享(一)
查看>>
Syniverse为亚洲100多家运营商提供一系列服务
查看>>
“光伏双反”与“201条款”有何差异?
查看>>
Android集成测试
查看>>
能源互联网将如何开拓光伏新市场?
查看>>