I want to combine all png files to a single PDF file. The following python script can make this task simpler. If you want to use the script, please make sure python can be installed and folder path and pdf file are correct.


#! /usr/local/bin/python3
# -*- coding: utf-8 -*-

from PIL import Image
import os

def combine2Pdf( folderPath, pdfFilePath ):
    files = os.listdir( folderPath )
    pngFiles = []
    sources = []
    for file in files:
        if 'png' in file:
            pngFiles.append( folderPath + file )
    pngFiles.sort()
    output = Image.open( pngFiles[0] )
    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 )

if __name__ == "__main__":
    folder = "/Users/weiyang/Desktop/pngFiles/"
    pdfFile = "/Users/weiyang/Desktop/contract.pdf"
    combine2Pdf( folder, pdfFile )

Categories: Python

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

join us
: You can get unlimited access to free membership content

X
0
Would love your thoughts, please comment.x
()
x