国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現簡單拆分PDF文件的方法

2019-11-25 17:06:50
字體:
來源:轉載
供稿:網友

本文實例講述了Python實現簡單拆分PDF文件的方法。分享給大家供大家參考。具體如下:

依賴pyPdf處理PDF文件
切分pdf文件

使用方法:
1)將要切分的文件放在input_dir目錄下
2)在configure.txt文件中設置要切分的份數(如要切分4份,則設置part_num=4)
3)執行程序
4)切分后的文件保存在output_dir目錄下
5)運行日志寫在pp_log.txt中

P.S. 本程序可以批量切割多個pdf文件

from pyPdf import PdfFileWriter, PdfFileReaderimport osimport timeimport sysdef 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, part_count_ye_end, fen, 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, output_file, config_count, f_w, now, file_name)    f_w.writelines('time: '+now+' file name: '+file_name+' status: success/n')pdf_main()

希望本文所述對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 衡山县| 涿鹿县| 奎屯市| 吉木萨尔县| 岳阳市| 楚雄市| 藁城市| 准格尔旗| 怀来县| 平武县| 永善县| 佛学| 台山市| 乌苏市| 临夏县| 浮梁县| 台湾省| 长丰县| 象山县| 兴安县| 仲巴县| 沙湾县| 青田县| 通山县| 新泰市| 额济纳旗| 新邵县| 公主岭市| 二连浩特市| 永安市| 宜良县| 年辖:市辖区| 多伦县| 深泽县| 富锦市| 连平县| 修水县| 郸城县| 东丰县| 沙河市| 龙口市|