pdf.image image 打印图像 for 循环中的相同图像

pdf.image image print image same image in for loop

我正在尝试将不同的照片输出到不同的 pdf 中 但我看到它仍然在所有 pdf 中打印相同的照片。

namelist=[] 值为 ['ABC1 XYZ1', 'ABC2 XYZ2', 'ABC3 XYZ3', 'ABC4 XYZ4', 'ABC5 XYZ5'] pics named same as in namelist 但它仍然只输出第一张图片。为什么? ABC2 XYZ2.pdf 请参阅 ABC2 XYZ2.pdf 中的此处。它应该输出 ABC2 XYZ2.jpg image.

from fpdf import FPDF
from main import *
import os
class PDF(FPDF):
    def lines(self):
        self.set_fill_color(255,99,71) # color for outer rectangle
        self.rect(5.0, 5.0, 200.0,287.0,'DF')
        self.set_fill_color(255,255,255) # color for inner rectangle
        self.rect(8.0, 8.0, 194.0,281.0,'FD')
    
    def imagex(self,sctplt,x,y,width,height):
        self.set_xy(x,y)
        self.image(sctplt, type='', w=width, h=height)
    def titles(self):
        self.set_xy(0.0,0.0)
        self.set_font('Arial', 'B', 16)
        self.set_text_color(220, 50, 50)
        self.cell(w=210.0, h=40.0, align='C', txt="t", border=0)
    def texts(self,name):
        with open(name,'rb') as xy:
            txt=xy.read().decode('latin-1')
        self.set_xy(10.0,80.0)    
        self.set_text_color(76.0, 32.0, 250.0)
        self.set_font('Arial', '', 12)
        self.multi_cell(0,10,txt)
        

namelist=[]
for k in range(len(x)):
    namelist.append(x[k][0])
print(namelist)

pdf = PDF(orientation='P', unit='mm', format='A4')
pdf.add_page()
pdf.lines()
image1="wisdom test/1.png"
image2="wisdom test/2.png"
pdf.imagex(image1,89.0,10.0,2000/50,1920/50)
pdf.imagex(image2,20.0,50.0,2000/50,1920/50)

for i in range(len(namelist)):
    currstudent=namelist[i]
    pdf.imagex("pics/{}.jpg".format(currstudent),22.0,52.5,1900/60,1850/55)
    pdf.output('{}.pdf'.format(currstudent),'F')
        
    
    pdf.titles()
    pdf.output('{}.pdf'.format(currstudent),'F')

您的代码仅创建一个只有一页的 PDF 文件。如果将所有 PDF 创建代码移动到 for i in range(len(namelist)): 循环中,您将为名单中的每个名称创建一个单独的 PDF 文件。类似于:

for i in range(len(namelist)):
    pdf = PDF(orientation='P', unit='mm', format='A4')
    pdf.add_page()
    pdf.lines()
    image1="wisdom test/1.png"
    image2="wisdom test/2.png"
    pdf.imagex(image1,89.0,10.0,2000/50,1920/50)
    pdf.imagex(image2,20.0,50.0,2000/50,1920/50)
    currstudent=namelist[i]
    pdf.imagex("pics/{}.jpg".format(currstudent),22.0,52.5,1900/60,1850/55)
    pdf.titles()    
    pdf.output('{}.pdf'.format(currstudent),'F')

我使用了一些示例图像并修改了您的代码,我能够生成单独的 PDF 文件,每个文件都有不同的图像。

前脚本运行目录列表:

$ ls . ./images/ ./output
.:
images    output    script.py

./images/:
one.jpg         small-left.jpg  small-right.jpg three.jpg       two.jpg

./output:

Post-script 运行 目录列表:(现在输出目录中有 3 个 .pdf 文件)

$ ls . ./images/ ./output
.:
images    output    script.py

./images/:
one.jpg         small-left.jpg  small-right.jpg three.jpg       two.jpg

./output:
one.pdf   three.pdf two.pdf

script.py

的内容
from fpdf import FPDF
import os

class PDF(FPDF):
    def lines(self):
        self.set_fill_color(255,99,71) # color for outer rectangle
        self.rect(5.0, 5.0, 200.0,287.0,'DF')
        self.set_fill_color(255,255,255) # color for inner rectangle
        self.rect(8.0, 8.0, 194.0,281.0,'FD')
    
    def imagex(self,sctplt,x,y,width,height):
        self.set_xy(x,y)
        self.image(sctplt, type='', w=width, h=height)
    def titles(self):
        self.set_xy(0.0,0.0)
        self.set_font('Arial', 'B', 16)
        self.set_text_color(220, 50, 50)
        self.cell(w=210.0, h=40.0, align='C', txt="t", border=0)
    def texts(self,name):
        with open(name,'rb') as xy:
            txt=xy.read().decode('latin-1')
        self.set_xy(10.0,80.0)    
        self.set_text_color(76.0, 32.0, 250.0)
        self.set_font('Arial', '', 12)
        self.multi_cell(0,10,txt)

for name in ["one", "two", "three"]:
    pdf = PDF(orientation='P', unit='mm', format='A4')
    pdf.add_page()
    pdf.lines()
    pdf.imagex("images/small-left.jpg",89.0,10.0,2000/50,1920/50)
    pdf.imagex("images/small-right.jpg",20.0,50.0,2000/50,1920/50)
    pdf.imagex(f"images/{name}.jpg",22.0,52.5,1900/60,1850/55)
    pdf.titles()
    pdf.output(f"output/{name}.pdf",'F')