醉酒思红颜ヽ 发表于 2020-9-11 13:25

【Python】将图片打包成pdf



from PIL import Image
import os
def combine2Pdf( folderPath, pdfFilePath ):
    files = os.listdir( folderPath )
    pngFiles = []
    sources = []
    for file in files:
      if 'jpg' in file:
            pngFiles.append( folderPath + file )
    pngFiles.sort()
    output = Image.open( pngFiles )
    pngFiles.pop( 0 )
    for file in pngFiles:
      pngFile = Image.open( file )
      if pngFile.mode == "RGB":
            pngFile = pngFile.convert( "RGB" )
      sources.append( pngFile )
    output.save( pdfFilePath, "pdf", save_all=True, append_images=sources )
    print('合成一个')

if __name__ == "__main__":
    #图片前缀,A文件路径,比如A文件夹里面有5个B文件,B文件里面全是图片,最好是有顺序的
    prefix="E:\\爬虫\\爬取的图片\\"
    #存放pdf的路径
    pdffix='E:\\爬虫\\'
    files = os.listdir(prefix)
    i=0
    for file in files:
      i=i+1
      combine2Pdf(prefix+file+'\\', peffix + file + '.pdf')
      print('共合成第',i)



未决 发表于 2020-9-11 13:25

6666

gongjuelan 发表于 2020-9-11 13:39

感谢楼主分享

全军过检测 发表于 2020-9-11 14:02

谢谢大牛

Shallum 发表于 2020-9-11 18:05

6666666666

will 发表于 2020-9-11 18:51

谢谢分享

lgysai 发表于 2020-9-12 07:59

多谢分享

liujiegame 发表于 2020-9-12 19:30

好的,非常感谢
页: [1]
查看完整版本: 【Python】将图片打包成pdf