如何将 qrc.py 中的图像访问到 reportlab?
How can I access images from qrc.py into reportlab?
我已将“image_fonts.qrc”转换为 image_fonts_rc.py 文件。它有一张名为“image.png”
的图像
如何将 qrc.py 文件中的图像用于 Python 中的 reportlab PDF。
文件image_fonts.qrc
<RCC>
<qresource prefix="image_fonts">
<file>image.png</file>
<file>logo.png</file>
</qresource>
</RCC>
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/image_fonts/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
我在上面使用了,但是我得到了一个错误。请找出以下错误。
TypeError: expected str, bytes or os.PathLike object, not QIcon
最小示例:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5 import uic
import sys
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, PageTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph, Flowable
import image_fonts_rc
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("test_images.ui", self)
self.show()
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/image_fonts/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
doc = SimpleDocTemplate("images.pdf", pagesize=A4, rightMargin=40, leftMargin=40, topMargin=20, bottomMargin=20, title ="Images")
width, height = A4
document = []
logo = icon
imgw = imgh = 80
im = (Image(logo, width=imgw, height=imgh))
document.append(im)
doc.build(document)
app = QApplication(sys.argv)
window = UI()
app.exec_()
不必使用 QPixmap 或 QIcon,但必须从图像中获取字节,如 :
from io import BytesIO
from PyQt5 import QtCore
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Image
import image_fonts_rc
def convert_qrc_to_bytesio(filename):
file = QtCore.QFile(filename)
if not file.open(QtCore.QIODevice.ReadOnly):
raise RuntimeError(file.errorString())
return
f = BytesIO(file.readAll().data())
return f
doc = SimpleDocTemplate(
"images.pdf",
pagesize=A4,
rightMargin=40,
leftMargin=40,
topMargin=20,
bottomMargin=20,
title="Images",
)
width, height = A4
document = []
logo = convert_qrc_to_bytesio(":/image_fonts/logo.png")
imgw = imgh = 80
im = Image(logo, width=imgw, height=imgh)
document.append(im)
doc.build(document)
我已将“image_fonts.qrc”转换为 image_fonts_rc.py 文件。它有一张名为“image.png”
的图像如何将 qrc.py 文件中的图像用于 Python 中的 reportlab PDF。
文件image_fonts.qrc
<RCC>
<qresource prefix="image_fonts">
<file>image.png</file>
<file>logo.png</file>
</qresource>
</RCC>
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/image_fonts/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
我在上面使用了,但是我得到了一个错误。请找出以下错误。
TypeError: expected str, bytes or os.PathLike object, not QIcon
最小示例:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtSql import *
from PyQt5 import uic
import sys
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, PageTemplate, TableStyle, Paragraph, Image, Spacer, Frame, Paragraph, Flowable
import image_fonts_rc
class UI(QMainWindow):
def __init__(self):
super(UI, self).__init__()
uic.loadUi("test_images.ui", self)
self.show()
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap(":/image_fonts/logo.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
doc = SimpleDocTemplate("images.pdf", pagesize=A4, rightMargin=40, leftMargin=40, topMargin=20, bottomMargin=20, title ="Images")
width, height = A4
document = []
logo = icon
imgw = imgh = 80
im = (Image(logo, width=imgw, height=imgh))
document.append(im)
doc.build(document)
app = QApplication(sys.argv)
window = UI()
app.exec_()
不必使用 QPixmap 或 QIcon,但必须从图像中获取字节,如
from io import BytesIO
from PyQt5 import QtCore
from reportlab.lib.pagesizes import A4
from reportlab.platypus import SimpleDocTemplate, Image
import image_fonts_rc
def convert_qrc_to_bytesio(filename):
file = QtCore.QFile(filename)
if not file.open(QtCore.QIODevice.ReadOnly):
raise RuntimeError(file.errorString())
return
f = BytesIO(file.readAll().data())
return f
doc = SimpleDocTemplate(
"images.pdf",
pagesize=A4,
rightMargin=40,
leftMargin=40,
topMargin=20,
bottomMargin=20,
title="Images",
)
width, height = A4
document = []
logo = convert_qrc_to_bytesio(":/image_fonts/logo.png")
imgw = imgh = 80
im = Image(logo, width=imgw, height=imgh)
document.append(im)
doc.build(document)