我正在尝试使用 ReportLab 从图像目录中创建一个 4 x 5 的图像网格(每页),其方式类似于摄影联系表。我需要保持图像的纵横比,还需要下面每个图像的文件名。
我最初是从使用 drawimage 并手动添加所有内容开始的,但现在我认为一个表格并将图像和文件名添加到每个单元格中可能是一种更好的方法。任何人都可以给我一些关于最好的方法的指示吗?
我花了几天时间试图弄清楚这一点,我想我正在绕圈子。先感谢您!
截至目前的脚本;
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib.pagesizes import A4
from PIL import Image
import os
def rowGen(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
def makePDF(document_title, data):
pdf = SimpleDocTemplate(
document_title,
pagesize = A4
)
style = TableStyle([
('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
('BOTTOMPADDING', (0, 0), (-1, -1), 12),
])
table = Table(data) #Creates and adds the data to the table
table.setStyle(style) #Sets style to the above defined style
elements = [] #Creates an empty elements list
elements.append(table) #Lays out the elements
pdf.build(elements)
path = 'Images/'
image_list = [i for i in os.listdir(path) if i.endswith('.jpg')] #list of images in selected directory
data = [i for i in rowGen(image_list, 4)]
makePDF('Test.pdf', data)