使用python对PDF文件进行等页数分割
用到了pyPDF库,很简单的脚步,废话不多说了。直接上代码
1 # coding:utf8 2 import os 3 from pyPdf import PdfFileWriter, PdfFileReader 4 def split(pdf_file, delta, output_dir): 5 if not os.path.exists(output_dir): 6 os.makedirs(output_dir) 7 if not os.path.exists(pdf_file): 8 return 9 input_stream = file(pdf_file, \'rb\') 10 pdf_input = PdfFileReader(input_stream) 11 page_count = pdf_input.getNumPages() 12 sum_page_count = int(page_count / (delta * 1.0)) 13 remind_page = page_count % delta 14 for i in range(0, sum_page_count + 1): 15 start = i * delta 16 end = (i + 1) * delta 17 pdf_out = PdfFileWriter() 18 file_path = os.path.join(output_dir, os.path.split(pdf_file)[1]) # 19 if i < sum_page_count: 20 file_path = os.path.join(output_dir, str(start + 1) + \'-\' + str(end) + ".pdf") 21 for j in range(start, end): 22 page = pdf_input.getPage(j) 23 pdf_out.addPage(page) 24 else: 25 file_path = os.path.join(output_dir, str(delta * sum_page_count + 1) + \'-\' + str(page_count) + ".pdf") 26 for j in range(delta * (sum_page_count), page_count): 27 page = pdf_input.getPage(j) 28 pdf_out.addPage(page) 29 out_stream = file(file_path, \'wb\') 30 pdf_out.write(out_stream) 31 out_stream.close() 32 input_stream.close() 33 34 if __name__ == \'__main__\': 35 import sys 36 #split(u\'C:/Users/sky/Desktop/17.pdf\', 50, u\'C:/Users/sky/Desktop/RES\') 37 try: 38 pdf_path = sys.argv[1] 39 page_count = int(sys.argv[2]) 40 out_dir = sys.argv[3] 41 split(pdf_path, page_count, out_dir) 42 except: 43 pass 44 45
版权声明:本文为AllStarGIS原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。