002_024 Python 在Windows和Mac OS X平台上统计PDF的文档的页数

来源:互联网 发布:cf手游领礼包软件 编辑:程序博客网 时间:2024/06/03 12:51

代码如下:

#encoding=utf-8print '中国'#在Windows和Mac OS X平台上统计PDF的文档的页数#Windows#借助于PyPdf#1.下载http://pybrary.net/pyPdf/pyPdf-1.13.zip#2.注册Python环境变量path中假如D:\Python27#3.cmd中 cd D:\Python27\pyPdf-1.13#4.setup.py install#5.安装库完成from pyPdf import PdfFileWriter, PdfFileReaderdef getPdfPageNum(path):    doc = PdfFileReader(file(path, "rb"))    return doc.getNumPages()print getPdfPageNum(r"C:\Users\Administrator\Desktop\books\byteofpython_chn.pdf")#Mac OS X 有自带内容 ,以下为参考代码import sysif sys.platform == 'darwin':        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( )

打印结果如下:

中国
110

0 0
原创粉丝点击