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

Blog


    10/14/2009

    [Python] PDF 分割與合併

    之前使用 Acrobat 9 可以正常的分割與合併 PDF,可是在頻繁的安全性更新之後,Acrobat 已經無法正常運作。網路上有一套免費的“PDF Split And Merge” 可以很漂亮的分割和合併 PDF 也支援批次功能,可惜我沒有看到支援 2-9 這種分割第2到9頁方式。

    遇到這種情況,我又想透過 Python 能否解決這個問題,網路上找到一篇相當好的開頭文章”Python PDF Split/Merge Library”,作者相當有趣,列了幾個 Python 的解決方法,不是透過 COM 、 Java 的元件,就是呼叫其他程式來完成,因為他也沒找到純 Python 的模組,所以他自己建了一個 pyPdf 專案,這是一個純 Python 的模組,看了官方範例可以滿足我的需要,用法也相當的簡單,我就使用這個模組來實作我的 PDF 分割與合併

    我的環境是 Vista+Python 2.6+pyPdf 1.12,完整程式碼可參考 smpdf.py

    這個程式透過讀取一個描述檔,內容如下:

    d:\temp\程序員雜誌2004年第12期.pdf
    d:\程序員雜誌2004年第12期-摘要.pdf
    6-8
    19

    格式為第一行是來源的 PDF 檔,第二行是目的 PDF 檔,接著是一系列要抓取的頁面順序,最終 PDF 內容就是依據頁面順序來產生。

    使用時只需要

    image

    就可以產生需要的 PDF,需要注意的是描述檔需要存成 UTF-8 編碼。

    有了這個程式,我又可以移除一些不必要的軟體了~

    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!3845.trak
    Weblogs that reference this entry
    • None