图片转换为pdf

import img2pdf
import os

class pdf:

    def __int__(self):
        pass

# 生成到一个pdf里面
    def from_all_photo_to_1pdf(self):
        photo_path = r'C:\Users\17160\PycharmProjects\yc_project\图片转换为pdf\图片\\'
        pdf_path = r'C:\Users\17160\PycharmProjects\yc_project\图片转换为pdf\pdf_result\\'
        photo_list = os.listdir(photo_path)
        print(photo_list)
        photo_list = [os.path.join(photo_path,i) for i in photo_list]
        print(photo_list)
        #生成pdf
        a4inpt = (img2pdf.mm_to_pt(720), img2pdf.mm_to_pt(1080))
        layout_fun = img2pdf.get_layout_fun(a4inpt)
        with open(pdf_path + '\\result.pdf', 'wb') as f:
            f.write(img2pdf.convert(photo_list, layout_fun=layout_fun))

    def from_1photo_to_1pdf(self):
        photo_path = r'C:\Users\17160\PycharmProjects\yc_project\图片转换为pdf\图片\\'
        pdf_path = r'C:\Users\17160\PycharmProjects\yc_project\图片转换为pdf\pdf_result\\'
        photo_list = os.listdir(photo_path)
        photo_list = [os.path.join(photo_path,i) for i in photo_list]
        tmp_list = list(range(1,1000))
        #print(tmp_list)
        #print(photo_list)
        for phote, num in zip(photo_list,tmp_list):
            a4inpt = (img2pdf.mm_to_pt(720), img2pdf.mm_to_pt(1080))
            layout_fun = img2pdf.get_layout_fun(a4inpt)
            with open(pdf_path + str(num) + ".pdf", "wb") as f:
                f.write(img2pdf.convert(phote, layout_fun=layout_fun))

yc = pdf()
yc.from_1photo_to_1pdf()

 

posted @ 2023-06-16 11:02  小菜鸟起飞ing  阅读(31)  评论(0编辑  收藏  举报