在python中使用fpdf创建pdf.无法将图像向右循环移动 [英] create pdf with fpdf in python. can not move image to the right in a loop

查看:76
本文介绍了在python中使用fpdf创建pdf.无法将图像向右循环移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用FPDF通过python生成pdf.我有一个正在寻找解决方案的问题.在文件夹图像"中,有一些图片我希望将其显示在一页上.我做到了-也许并不优雅.不幸的是,我无法将图片向右移动.看来pdf_set_y不会在循环中起作用.

I use FPDF to generate a pdf with python. i have a problem for which i am looking for a solution. in a folder "images", there are pictures that I would like to display each on ONE page. I did that - maybe not elegant. unfortunately i can't move the picture to the right. it looks like pdf_set_y won't work in the loop.

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

如果您对我有解决方案/帮助,那就太好了.非常感谢问候亚历克斯

Would be great if you have a solution/help for me. Thanks alot greets alex

推荐答案

我看到了这个问题.您要在对 pdf.image()的调用中指定 x y 位置.该评估基于此处的 image 文档:

I see the issue. You want to specify the x and y location in the call to pdf.image(). That assessment is based on the documentation for image here: https://pyfpdf.readthedocs.io/en/latest/reference/image/index.html

因此,您可以改为执行此操作(仅在此处显示 for 循环):

So you can instead do this (just showing for loop here):

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 -> ^^^^^  ^^^^

这篇关于在python中使用fpdf创建pdf.无法将图像向右循环移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆