2007-07-30

 

Python Cookbook 2.24 在Mac OS系统上统计PDF文档的页数

需求:

假如你的系统是Mac OS(10.3或以后),你希望统计一个PDF文档的页数.

讨论:

PDF格式和Python都是Mac OS默认支持的,实现本节的需求异常简单:

#!/usr/bin python
import CoreGraphics
def pageCount(pdfPath):
    "Return the number of pages for the PDF document at the given path."
    pdf = CoreGraphics.CGPDFDocumentCreateWithProvider(
              CoreGraphics.CGDataProviderCreateWithFilename(pdfPath)
          )
    return pdf.getNumberOfPages( )
if _ _name_ _ == '_ _main_ _':
    import sys
    for path in sys.argv[1:]:
        print pageCount(path)

另一个解决方法是使用Python的PyObjC扩展,这样能使代码在Mac OS的Foundation和AppKit框架下都能工作,这样你的脚本在比较旧的Mac  OS版本上也能使用了, 比如10.2 Jaguar.在10.3以后的Mac OS版本中,请确保Python安装在你的系统下,你才能使用一些比较有用的Python模块,比如CoreGraphics,这样你才能用一些Apple's的Quartz图形引擎.

相关说明:

我没有见过Mac OS,本节的内容基本不明白,立此存照吧.

Comments: 发表评论



<< Home

This page is powered by Blogger. Isn't yours?