如何使用reportlab将数千张图像添加到pdf中
我有一千个PNG图像,并使用reportlab添加 将这些图像转换为PDF, 代码是: def PICU出口(PICU路径):如何使用reportlab将数千张图像添加到pdf中,pdf,reportlab,Pdf,Reportlab,我有一千个PNG图像,并使用reportlab添加 将这些图像转换为PDF, 代码是: def PICU出口(PICU路径): 如果操作系统路径存在(pic\u路径): #打印('pic_exits') #打印(图片路径) target=PIL.Image.open(pic\u路径) #fp=打开(pic_路径,'rb') #target=PIL.Image.open(fp) #fp.close() targetx,targety=target.size 删除目标 #打印(targetx、tar
如果操作系统路径存在(pic\u路径):
#打印('pic_exits')
#打印(图片路径)
target=PIL.Image.open(pic\u路径)
#fp=打开(pic_路径,'rb')
#target=PIL.Image.open(fp)
#fp.close()
targetx,targety=target.size
删除目标
#打印(targetx、targety)
tx,ty=get_xy(targetx,targety)
targetimg=图像(图像路径,tx,ty)
其他:
targetimg=段落(“不存在”,正文样式)
返回目标
def draw_sourcepic(自我目录,pic_列表):
pic=[pic_list[i:i+3]表示范围内的i(0,len(pic_list),3)]
数据=[]
i=0
对于pic中的val:
每条线=[]
对于val中的PICU路径:
i+=1
pic_image=pic_出口(pic_路径)
eachline.append(图片)
而len(eachline)<3:
eachline.append(无)
data.append(eachline)
PICU表格=图纸表格(数据,(150150),无)
故事。附加(pic_表)
但其存在以下错误:
IOError:无法打开资源“C:\workspace\decode\2012\result\pic\510.png
这可能意味着我打开了太多的图片
官方文件说,
图像(数字图片)。支持PIL/Java 1.4(Python/Java图像库)支持的格式。目前,作为可流动图像的图像总是水平居中
在框架中,我们允许两种懒散,以允许文档中有许多图像
这可能会导致文件句柄不足。
lazy=1在需要时才打开映像。
lazy=2在需要时打开映像,然后关闭它
但当我换成这个的时候:
targetimg=Image(pic_路径,tx,ty,lazy=2)
其具有以下错误:
AttributeError:。\u文件
如何将数千张图像添加到PDF中?问题很长,我知道这已经晚了一年,但我最近遇到了类似的问题。我认为这是reportlab的问题——当lazy=2时,图像所需的文件属性被销毁后会再次调用。我已经报告了该问题和我的解决方法。希望能有所帮助
if os.path.exists(pic_path):
#print ('pic_exits')
#print (pic_path)
target = PIL.Image.open(pic_path)
#fp = open(pic_path,'rb')
#target = PIL.Image.open(fp)
#fp.close()
targetx,targety = target.size
del target
#print (targetx,targety)
tx,ty = get_xy(targetx,targety)
targetimg = Image(pic_path, tx, ty)
else:
targetimg = Paragraph('<para align = center>not exist</para>',Bodystyle)
return targetimg
pic = [pic_list[i:i+3] for i in range(0,len(pic_list),3)]
data = []
i = 0
for val in pic:
eachline = []
for pic_path in val:
i+=1
pic_image = pic_exits(pic_path)
eachline.append(pic_image)
while len(eachline) < 3:
eachline.append(None)
data.append(eachline)
pic_table = drawTable(data,(150,150,150),None)
Story.append(pic_table)