简单拆分PDF文件,将一个PDF文件拆分成指定份数
发布时间:2020-05-24 23:35:47 所属栏目:Python 来源:互联网
导读:简单拆分PDF文件,将一个PDF文件拆分成指定份数
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 from pyPdf import PdfFileWriter,PdfFileReader
import os
import time
import sys
def part_pdf(input_file,output_file,config_count,f_w,now,file_name):
file1 = file(input_file,'rb')
pdf = PdfFileReader(file1)
pdf_pages_len = len(pdf.pages)
if config_count <= pdf_pages_len:
ye = pdf_pages_len / config_count
lst_ye = pdf_pages_len % config_count
part_count = 0
part_count_ye = 0
for fen in range(config_count):
part_count += 1
if part_count == config_count:
part_ye = ye + lst_ye
else:
part_ye = ye
write_pdf(pdf,part_count_ye,part_count_ye+part_ye,fen,output_file)
part_count_ye += ye
else:
f_w.writelines('time: '+now+' file name: '+file_name+' status: part_num > pdf pages [error]n')
sys.exit(1)
def write_pdf(pdf,part_count_ye_end,output_file):
out = PdfFileWriter()
for pp in range(part_count_ye,part_count_ye_end):
out.addPage(pdf.getPage(pp))
ous = file(output_file+'_'+str(fen+1)+'.pdf','wb')
out.write(ous)
ous.close()
def pdf_main():
f = open('configure.txt','r')
f_w = open('pp_log.txt','a')
now = time.strftime('%Y-%m-%d %H:%M:%S')
for i in f:
i_ = i.strip()
aa = i_.split('=')[1]
if i_.find('part_num=') != -1 and aa.isdigit():
config_count = int(aa)
else:
f_w.writelines('time: '+now+' status: part_num in configure.txt is error [error]n')
sys.exit(1)
files = os.listdir('input_dir/')
for each in files:
input_file = 'input_dir/'+each
file_name = input_file[input_file.index('/'):input_file.index('.')]
output_file = 'output_dir/'+file_name
part_pdf(input_file,file_name)
f_w.writelines('time: '+now+' file name: '+file_name+' status: successn')
pdf_main()
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
