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 )
    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


Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page