0

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

namelist=[]值是['ABC1 XYZ1', 'ABC2 XYZ2', 'ABC3 XYZ3', 'ABC4 XYZ4', 'ABC5 XYZ5'] 与名称列表中命名相同的图片, 但它仍然只输出第一张图片。为什么? ABC2 XYZ2.pdf 见此处ABC2 XYZ2.pdf。它应该输出 ABC2 XYZ2.jpg 图像。

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')

4

1 回答 1

1

您的代码仅创建一个包含一页的 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:

后脚本运行目录列表:(现在输出目录中有 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')
于 2021-07-29T12:22:45.407 回答