Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中将png图像转换为一个pdf_Python_Pdf_Png_Fpdf - Fatal编程技术网

在python中将png图像转换为一个pdf

在python中将png图像转换为一个pdf,python,pdf,png,fpdf,Python,Pdf,Png,Fpdf,我有一个.png图像列表。我需要将它们全部转换成一个pdf格式,每页9张图片,但不是将它们一个接一个地垂直放置,而是填充所有宽度,然后继续下一行。 每次的图片数量可能不同(12,…15) 我试过fpdf from fpdf import FPDF list_of_images = [1.png, 2.png, ... 15.png] w = 70 h = 60 pdf = FPDF(orientation = 'L') for image in list_of_images: pdf.

我有一个.png图像列表。我需要将它们全部转换成一个pdf格式,每页9张图片,但不是将它们一个接一个地垂直放置,而是填充所有宽度,然后继续下一行。 每次的图片数量可能不同(12,…15)

我试过fpdf

from fpdf import FPDF

list_of_images = [1.png, 2.png, ... 15.png]
w = 70
h = 60
pdf = FPDF(orientation = 'L')
for image in list_of_images:
    pdf.image(image, w=sizew, h=sizeh)
pdf.output("Memory_usage.pdf", "F")
还有wkhtmltopdf

template = Template('''<!doctype html>
<html>
<body>
        <div style="display: flex; flex-direction: row">
        {% for image in images %}
            <img src="{{ image }}" />
        {% endfor %}
        </div>
</body>
</html>''')
list_of_images = [1.png, 2.png, ... 15.png]
html = template.render(images=list_of_pict)
with open("my_new_file.html", "wb") as fh:
    fh.write(html)

p = subprocess.Popen(['wkhtmltopdf', '-', 'Memory_usage.pdf'], stdin=subprocess.PIPE, universal_newlines=True)
p.communicate(html)
p.wait()
template=模板(“”)
{%用于图像中的图像%}
{%endfor%}
''')
图片列表=[1.png,2.png,…15.png]
html=template.render(图像=图片列表)
打开(“my_new_file.html”、“wb”)作为fh:
fh.write(html)
p=subprocess.Popen(['wkhtmltopf','-','Memory\u usage.pdf'],stdin=subprocess.PIPE,universal\u newlines=True)
p、 通信(html)
p、 等等

但它们都将每张图片一张一张地放在另一张下面

只需使用FPDF将每张图片放在所需的坐标处:

pdf.image(image, x=50, y=100, w=sizew, h=sizeh)
有关FPDF文档的更多信息: