x详情参考Qt官网
参考:https://blog.csdn.net/jia666666/article/details/81590005
https://www.jianshu.com/p/bb8e9b3ad9f7
https://www.jianshu.com/p/8298c2474630
流程:实例化Qprinter --》设置打印弹出QprintDialog --》调用文本的打印方法.print()
例子:打印图像
#coding=utf-8
import sys
from PyQt5.QtWidgets import QWidget,QLabel,QAction,QCheckBox,QApplication,QMainWindow,QGridLayout
from PyQt5.QtCore import Qt,QRect
from PyQt5.QtGui import QPainter,QPixmap,QImage
#打印支持模块,Qprint打印模块,QprintDialog调出打印窗口,QpagesteupDialog调出打印设置窗口
from PyQt5.QtPrintSupport import QPrinter,QPrintDialog,QPageSteupDialog
class SignalDemo(QMainWindow):
def __init__(self):
super().__init__()
self.initGUI()
def initGUI(self):
self.setGeometry(300,300,300,200)
widget=QWidget()
grid=QGridLayout()
widget.setLayout(grid)
self.setCentralWidget(widget)
#生成menubar栏
self.createBar()
#label
#加载图片
self.img=QImage('./a.ico')
self.label=QLabel()
grid.addWidget(self.label,0,0)
self.label.setPixmap(QPixmap.fromImage(self.img))
self.show()
def createBar(self): #生成menubar
file=self.menuBar().addMenu(self.tr('File'))
da=QAction('打印',self)
da.setShortcut('Ctrl+p')
file.addAction(da)
da.triggered.connect(self.showPrint) #信号槽
def showPrint(self):
#实例化打印图像对象
printer=QPrinter()
#打印窗口弹出
printdialog=QPrintDialog(printer,self)
#绘制
if printdialog.exec_():
#调用绘图
painter=QPainter(printer)
#实例化视图窗口
rect=painter.viewport()
#获取图片的尺寸
size=self.img.size()
#Qt.KeepAspectRatio 保持纵横比
size.scale(rect.size(),Qt.KeepAspectRatio)
#设置窗口的大小为图片的磁村,并在窗口内绘制
painter.setViewport(rect.x(),rect.y(),size.width(),size.height())
painter.setWindow(self.img.rect)
painter.drawImage(0,0,self.img)
if __name__=='__main__':
app=QApplication(sys.argv)
demo=SignalDemo()
sys.exit(app.exec_())
例二:
import sys
from PyQt5.QtWidgets import QApplication,QLabel,QMainWindow,QFormLayout,QWidget,QAction
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.QtGui import QTextCursor,QTextDocument,QImage,QPixmap
class printDemo(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('打印demo')
self.setGeometry(300,300,400,300)
widget=QWidget()
self.setCentralWidget(widget)
layout=QFormLayout()
widget.setLayout(layout)
self.img=QImage('./a.ico')
label=QLabel()
label.setPixmap(QPixmap.fromImage(self.img))
layout.addRow('图片打印',label)
self.text='tststtas飒飒嘎嘎嘎啥归属感'
label=QLabel()
label.setText(self.tr(self.text))
layout.addRow('文章打印',label)
file=self.menuBar().addMenu('File')
action1=QAction('转为pdf',self)
action1.setShortcut('Ctrl+p')
file.addAction(action1)
action1.triggered.connect(self.printShow)
def printShow(self):
printer=QPrinter()
#设置打印输出的形式:可以设置为输出pdf格式
printer.setOutputFormat(QPrinter.PdfFormat)
#设置输出的路径为当前
printer.setOutputFileName('test.pdf')
#实例化一个富文本
document=QTextDocument()
#实例化一个文本指针并指向document的光标
cursor=QTextCursor(document)
#富文本添加内容
cursor.insertImage(self.img)
cursor.insertText(self.text)
#调用print()方法 参数为当前实例化的打印函数
document.print(printer)
if __name__=='__main__':
app=QApplication(sys.argv)
demo=printDemo()
demo.show()
sys.exit(app.exec())
例三:1,调用打印对话框,2,调用打印预览框3,直接打印,4打印到Pdf
import sys
from PyQt5.QtWidgets import QApplication,QLabel,QMainWindow,QFormLayout,QDialog,QWidget,QAction
from PyQt5.QtPrintSupport import QPrinter,QPrintDialog,QPrintPreviewDialog
from PyQt5.QtGui import QTextCursor,QTextDocument,QImage,QPixmap
#需要打印的文字
text='11111111111wwwwwwwwwww呜呜呜呜呜呜呜呜无无无无'
class printDemo(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('打印demo')
self.setGeometry(300,300,300,200)
self.createBar()
#设置menuBar
def createBar(self):
bar=self.menuBar()
printmenu=bar.addMenu('打印')
#直接打印不显示
printAction1=QAction('直接打印不显示',self)
printmenu.addAction(printAction1)
printAction1.triggered.connect(self.no_showPrinter)
#打印弹出设置对话框
printAction2=QAction('打印弹出设置',self)
printmenu.addAction(printAction2)
printAction2.triggered.connect(self.show_steupPrinter)
#打印弹出预览对话框
printAction3=QAction('打印弹出预览',self)
printmenu.addAction(printAction3)
printAction3.triggered.connect(self.show_previewPrinter)
#打印输出pdf
printAction4=QAction('打印输出pdf',self)
printmenu.addAction(printAction4)
printAction4.triggered.connect(self.show_pdfPrinter)
#打印不显示
def no_showPrinter(self):
printer=QPrinter()
self.handlePaintRequest(printer)
#打印显示设置
def show_steupPrinter(self):
printer=QPrinter()
printDialog=QPrintDialog(printer)
if printDialog.exec_()==QDialog.Accepted:
self.handlePaintRequest(printer)
#打印预览
# ------------------这个比较特殊-----------------------------------
def show_previewPrinter(self):
dialog=QPrintPreviewDialog()
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
-
#输出pdf格式不打印
def show_pdfPrinter(self):
printer=QPrinter()
printDialog=QPrintDialog(printer)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName('a.pdf')
self.handlePaintRequest(printer)
#打印函数
def handlePaintRequest(self,printer):
#流程将文本或图片,其他写入QTextdocument中,然后调用print函数
document=QTextDocument()
cursor=QTextCursor(document)
cursor.insertText(text)
document.print(printer)
if __name__=='__main__':
app=QApplication(sys.argv)
demo=printDemo()
demo.show()
sys.exit(app.exec())