Img2pdf:python图片转pdf库

可以直接转换列表;

with open("name.pdf","wb") as f:
	f.write(img2pdf.convert(imgs))

2.软件安装 pip install img2pdf

3.软件使用范例 如下是在 img2pdf 给出的应用范例。它显示了在python应用img2pdf的多种格式。

import img2pdf

opening from filename

with open(“name.pdf”,“wb”) as f: f.write(img2pdf.convert(‘test.jpg’))

opening from file handle

with open(“name.pdf”,“wb”) as f1, open(“test.jpg”) as f2: f1.write(img2pdf.convert(f2))

using in-memory image data

with open(“name.pdf”,“wb”) as f: f.write(img2pdf.convert(”\x89PNG…“)

multiple inputs (variant 1)

with open(“name.pdf”,“wb”) as f: f.write(img2pdf.convert(“test1.jpg”, “test2.png”))

multiple inputs (variant 2)

with open(“name.pdf”,“wb”) as f: f.write(img2pdf.convert([“test1.jpg”, “test2.png”]))

convert all files ending in .jpg inside a directory

dirname = “/path/to/images” with open(“name.pdf”,“wb”) as f: imgs = [] for fname in os.listdir(dirname): if not fname.endswith(“.jpg”): continue path = os.path.join(dirname, fname) if os.path.isdir(path): continue imgs.append(path) f.write(img2pdf.convert(imgs))

convert all files ending in .jpg in a directory and its subdirectories

dirname = “/path/to/images” with open(“name.pdf”,“wb”) as f: imgs = [] for r, _, f in os.walk(dirname): for fname in f: if not fname.endswith(“.jpg”): continue imgs.append(os.path.join(r, fname)) f.write(img2pdf.convert(imgs))

convert all files matching a glob

import glob with open(“name.pdf”,“wb”) as f: f.write(img2pdf.convert(glob.glob(“/path/to/*.jpg”)))

writing to file descriptor

with open(“name.pdf”,“wb”) as f1, open(“test.jpg”) as f2: img2pdf.convert(f2, outputstream=f1)

specify paper size (A4)

a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297)) layout_fun = img2pdf.get_layout_fun(a4inpt) with open(“name.pdf”,“wb”) as f: f.write(img2pdf.convert(‘test.jpg’, layout_fun=layout_fun)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

裁判系统中的信标系统的使用 ———————————————— 版权声明:本文为CSDN博主「卓晴」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/zhuoqingjoking97298/article/details/110222668

https://blog.csdn.net/zhuoqingjoking97298/article/details/110222668