Chui-Wen's profileChui-Wen Chiu's NoteBlogListsGuestbookMore Tools Help

Blog


    11/1/2009

    [Python] 圖片合併成 PDF

    最近清理電腦中的文件,發現一堆從電子書擷取下來的連續 jpg 圖檔,原本想用 Acrobat 合併,可是上次電腦重灌後我就沒灌,原本上網找了套 Image2PDF 來處理可惜要付費,不然只能轉單一檔案,真是 OOXX。算了,嘗試用 Python 來解決,有上次分割/合併 PDF 的經驗,我原以為透過 pyPDF 可以輕鬆達成,可惜 pyPDF 不支援新增一張圖片方式來建立 PDF。於是我迂迴的解決這個問題,也就是透過 PIL 這個模組將圖片先產生 PDF 然後在將 PDF 透過 pyPDF 合併 就可以達成我的需求 XD

    完整程式碼可在 Google Code 找到

    我簡單的描述這個腳本要怎麼運作,首先承襲之前 smpdf.py  的作法,也就是透過一個簡單的描述檔來說明輸出入的檔案,描述檔格式相當的簡單,第一行是輸出 PDF 檔案的完整路徑,藉著是一系列要合併的圖檔,每個檔案一行,且檔案要存成 UTF-8,千萬不要用 Windows 內建的記事本,因為他會存成 UTF-8+BOM 格式,

    範例的描述檔可參考 cfg.txt

    有了描述檔之後就可執行 python.exe im2pdf.py cfg.txt 就會出現如下的畫面

    image

    然後你就會得到一個建立完成的 PDF,夠簡單吧 XD

    image

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://chuiwenchiu.spaces.live.com/blog/cns!CA5D9227DF9E78E8!4754.trak
    Weblogs that reference this entry
    • None