我使用 FPDF 通过 python 生成 pdf。我有一个问题正在寻找解决方案。在“图像”文件夹中,我想在一页上显示每张图片。我就是这么做的——也许并不优雅。不幸的是我无法将图片移到右侧。看起来 pdf_set_y 在循环中不起作用。
from fpdf import FPDF
from os import listdir
from os.path import isfile, join
onlyfiles = [f for f in listdir('../images') if isfile(join('../images', f))]
pdf = FPDF('L')
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
onlyfiles = ['VI 1.png', 'VI 2.png', 'VI 3.png']
y = 10
for image in onlyfiles:
pdf.set_x(10)
pdf.set_y(y)
pdf.cell(10, 10, 'please help me' + image, 0, 0, 'L')
y = y + 210 #to got to the next page
pdf.set_x(120)
pdf.set_y(50)
pdf.image('../images/' + image, w=pdf.w/2.0, h=pdf.h/2.0)
pdf.output('problem.pdf', 'F')
Run Code Online (Sandbox Code Playgroud)
如果您能为我提供解决方案/帮助,那就太好了。非常感谢问候亚历克斯
我看到了这个问题。您想要在调用中指定x和位置。该评估基于此处的文档:https://pyfpdf.readthedocs.io/en/latest/reference/image/index.htmlypdf.image()image
所以你可以这样做(仅for在此处显示循环):
for image in onlyfiles:
pdf.set_x(10)
pdf.set_y(y)
pdf.cell(10, 10, 'please help me' + image, 0, 0, 'L')
y = y + 210 # to go to the next page
# increase `x` from 120 to, say, 150 to move the image to the right
pdf.image('../images/' + image, x=120, y=50, w=pdf.w/2.0, h=pdf.h/2.0)
# new -> ^^^^^ ^^^^
Run Code Online (Sandbox Code Playgroud)