Python 3.x python将PIL图像添加到pdf文件中
如何将qrcode库创建的PIL图像添加到PDF文件中。下面是我得到的代码和错误消息Python 3.x python将PIL图像添加到pdf文件中,python-3.x,pdf-generation,qr-code,Python 3.x,Pdf Generation,Qr Code,如何将qrcode库创建的PIL图像添加到PDF文件中。下面是我得到的代码和错误消息 > def WriteQRCodeToPDF: > p = canvas.Canvas("document-output.pdf") > pagesize=letter > width, height = letter > image = ImageReader(GenerateQRCode("data")); > p.drawImag
> def WriteQRCodeToPDF:
> p = canvas.Canvas("document-output.pdf")
> pagesize=letter
> width, height = letter
> image = ImageReader(GenerateQRCode("data"));
> p.drawImage(image, inch, height - 2 * inch, 100,100);
> p.showPage()
> p.save()
>
> def GenerateQRCode(qrdata):
> qr = qrcode.QRCode(
> version=1,
> error_correction=qrcode.constants.ERROR_CORRECT_L,
> box_size=10,
> border=4,
> )
> qr.add_data(qrdata)
> qr.make(fit=True)
> img = qr.make_image()
> return img
错误消息如下所示。似乎ImageReader不接受PIL图像作为参数。我可以用什么来代替它
> Traceback (most recent call last): File
> "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 657, in open_for_read
> return open_for_read_by_name(name,mode) File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 601, in open_for_read_by_name
> return open(name,mode) TypeError: invalid file: <qrcode.image.pil.PilImage object at 0xb6a9ccac>
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last): File
> "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 660, in open_for_read
> return getBytesIO(datareader(name) if name.startswith('data:') else urlopen(name).read()) File
> "/usr/local/lib/python3.4/dist-packages/qrcode/image/pil.py", line 35,
> in __getattr__
> return getattr(self._img, name) File "/usr/local/lib/python3.4/dist-packages/PIL/Image.py", line 610, in
> __getattr__
> raise AttributeError(name) AttributeError: startswith
>
> During handling of the above exception, another exception occurred:
>
> Traceback (most recent call last): File "./qrcodegenerator.py", line
> 44, in <module>
> main() File "./qrcodegenerator.py", line 41, in main
> WriteQRCodeToPDF(dataList) File "./qrcodegenerator.py", line 21, in WriteQRCodeToPDF
> image = ImageReader(GenerateQRCode("data1")); File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 800, in __init__
> annotateException('\nfileName=%r identity=%s'%(fileName,self.identity())) File
> "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 1380, in annotateException
> rl_reraise(t,v,b) File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 137, in rl_reraise
> raise v File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 764, in __init__
> self.fp = open_for_read(fileName,'b') File "/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py", line
> 662, in open_for_read
> raise IOError('Cannot open resource "%s"' % name) OSError: Cannot open resource "<qrcode.image.pil.PilImage object at 0xb6a9ccac>"
> fileName=<qrcode.image.pil.PilImage object at 0xb6a9ccac>
> identity=[ImageReader@0xb6a9cc2c]
>回溯(最近一次调用上次):文件
>“/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py”,第行
>657,开放阅读
>返回open_for_read_by_name(name,mode)文件“/usr/local/lib/python3.4/dist packages/reportlab/lib/utils.py”,第行
>601,在“打开”中,按名称读取
>返回打开(名称、模式)类型错误:无效文件:
>
>在处理上述异常期间,发生了另一个异常:
>
>回溯(最近一次调用上次):文件
>“/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py”,第行
>660,打开供读取
>如果name.startswith('data:')或者urlopen(name.read())文件,则返回getBytesIO(datareader(name))
>“/usr/local/lib/python3.4/dist packages/qrcode/image/pil.py”,第35行,
>在__
>返回getattr(self._img,name)文件“/usr/local/lib/python3.4/dist packages/PIL/Image.py”,第610行,在
>_uuugetattr__
>引发AttributeError(名称)AttributeError:StartWith
>
>在处理上述异常期间,发生了另一个异常:
>
>回溯(最后一次调用):文件“/qrcodegenerator.py”,第行
>44,在
>main()文件“/qrcodegenerator.py”,第41行,在main中
>WriteQRCodeToPDF(数据列表)文件“/qrcodegenerator.py”,第21行,WriteQRCodeToPDF
>image=ImageReader(GenerateQRCode(“data1”));文件“/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py”,第行
>800,在初始位置__
>注释异常('\n文件名=%r标识=%s%%(文件名,self.identity()))文件
>“/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py”,第行
>1380年,例外
>rl_reraise(t,v,b)文件“/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py”,第行
>137,在里拉
>raisev文件“/usr/local/lib/python3.4/dist-packages/reportlab/lib/utils.py”,第行
>764,在初始__
>self.fp=open_for_read(文件名,'b')文件“/usr/local/lib/python3.4/dist packages/reportlab/lib/utils.py”,第行
>662,处于打开状态,以便读取
>raise IOError('无法打开资源“%s”'%name)OSError:无法打开资源“”
>文件名=
>身份=[ImageReader@0xb6a9cc2c]
我使用了QRCode()
这是一段适合我的代码:
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="backup_key.pdf"'
p = canvas.Canvas(response)
img = qrcode.make(data)
image = ImageReader(img._img)
p.drawImage(image, 10, 10)
错误消息在哪里?我刚刚添加了它