Last active
April 19, 2017 07:25
-
-
Save WyattJia/5188e33fc5a3edad0636bacbd6d79c63 to your computer and use it in GitHub Desktop.
统计同一个目录下多个 PDF 文件页数的一段代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- author:wellls -*- | |
import re, os, glob, sys | |
rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL) | |
def count_pages(filename): | |
data = file(filename,"rb").read() | |
return len(rxcountpages.findall(data)) | |
def sum_pages(args): | |
if len(args) > 1: | |
if args[1].endswith(".pdf"): | |
return count_pages(args[1]) | |
else: | |
os.chdir(args[1]) | |
total_pages = 0 | |
fnames = glob.glob("**/*.pdf") | |
for fname in fnames: | |
total_pages = total_pages + count_pages(fname) | |
return total_pages | |
if __name__=="__main__": | |
print(sum_pages(sys.argv)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment