欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)簡(jiǎn)單拆分PDF文件的方法

 更新時(shí)間:2015年07月30日 12:09:04   作者:willzhao  
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單拆分PDF文件的方法,可實(shí)現(xiàn)將一個(gè)PDF文件拆分成指定份數(shù)的功能,涉及pyPdf模塊的使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)簡(jiǎn)單拆分PDF文件的方法。分享給大家供大家參考。具體如下:

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

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

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

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, 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()

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 基于Python制作一個(gè)文件解壓縮工具

    基于Python制作一個(gè)文件解壓縮工具

    經(jīng)常由于各種壓縮格式的不一樣用到文件的解壓縮時(shí)就需要下載不同的解壓縮工具去處理不同的文件。本文將用Python制作一個(gè)解壓縮小工具,以后再也不用下載各種格式的解壓縮軟件了
    2022-05-05
  • 視覺直觀感受若干常用排序算法

    視覺直觀感受若干常用排序算法

    這篇文章主要利用視覺直觀的幾種若干常用排序算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Python實(shí)現(xiàn)多線程HTTP下載器示例

    Python實(shí)現(xiàn)多線程HTTP下載器示例

    本篇文章主要介紹了Python實(shí)現(xiàn)多線程HTTP下載器示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • python和c語言的主要區(qū)別總結(jié)

    python和c語言的主要區(qū)別總結(jié)

    在本篇文章里小編給各位整理了關(guān)于python和c語言的主要區(qū)別的相關(guān)知識(shí)帖內(nèi)容,有需要的朋友們學(xué)習(xí)閱讀下。
    2019-07-07
  • Async?IO在Python中的異步編程工作實(shí)例解析

    Async?IO在Python中的異步編程工作實(shí)例解析

    這篇文章主要為大家介紹了Async?IO在Python中的異步編程工作實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • python算法表示概念掃盲教程

    python算法表示概念掃盲教程

    這篇文章主要為大家詳細(xì)介紹了python算法表示概念掃盲教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Python實(shí)現(xiàn)json對(duì)值進(jìn)行模糊搜索的示例詳解

    Python實(shí)現(xiàn)json對(duì)值進(jìn)行模糊搜索的示例詳解

    我經(jīng)常使用json進(jìn)行存儲(chǔ)配置,于是常常遇到這樣的問題:如果想要對(duì)某個(gè)數(shù)組里的值進(jìn)行模糊搜索,同時(shí)輸出相關(guān)的其他數(shù)組相同位置的的值該如何實(shí)現(xiàn)呢?本文就來和大家詳細(xì)聊聊
    2023-01-01
  • 淺析Python的Django框架中的Memcached

    淺析Python的Django框架中的Memcached

    這篇文章主要介紹了淺析Python的Django框架中的緩存機(jī)制,其中著重講到了Memcached,需要的朋友可以參考下
    2015-07-07
  • Python實(shí)現(xiàn)對(duì)照片中的人臉進(jìn)行顏值預(yù)測(cè)

    Python實(shí)現(xiàn)對(duì)照片中的人臉進(jìn)行顏值預(yù)測(cè)

    今天給大家?guī)淼氖顷P(guān)于Python實(shí)戰(zhàn)的相關(guān)知識(shí),文章圍繞如何用Python實(shí)現(xiàn)對(duì)照片中的人臉進(jìn)行顏值預(yù)測(cè)展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • python常見問題之ModuleNotFoundError: No module named ‘rest_framework‘解決

    python常見問題之ModuleNotFoundError: No module nam

    這篇文章主要介紹了python常見問題之ModuleNotFoundError: No module named ‘rest_framework‘解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-07-07

最新評(píng)論