首頁 後端開發 Python教學 Python实现简单拆分PDF文件的方法

Python实现简单拆分PDF文件的方法

Jun 10, 2016 pm 03:08 PM
pdf文件 python 分割

本文实例讲述了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, 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()

登入後複製

希望本文所述对大家的Python程序设计有所帮助。

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

倉庫:如何復興隊友
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

怎麼下載deepseek 小米 怎麼下載deepseek 小米 Feb 19, 2025 pm 05:27 PM

怎麼下載deepseek 小米

模板化的優點和缺點有哪些? 模板化的優點和缺點有哪些? May 08, 2024 pm 03:51 PM

模板化的優點和缺點有哪些?

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2 Jul 01, 2024 am 07:22 AM

Google AI 為開發者發佈 Gemini 1.5 Pro 和 Gemma 2

只要250美元,Hugging Face技術主管手把手教你微調Llama 3 只要250美元,Hugging Face技術主管手把手教你微調Llama 3 May 06, 2024 pm 03:52 PM

只要250美元,Hugging Face技術主管手把手教你微調Llama 3

分享幾個.NET開源的AI和LLM相關專案框架 分享幾個.NET開源的AI和LLM相關專案框架 May 06, 2024 pm 04:43 PM

分享幾個.NET開源的AI和LLM相關專案框架

golang 函數調試與分析的完整指南 golang 函數調試與分析的完整指南 May 06, 2024 pm 02:00 PM

golang 函數調試與分析的完整指南

deepseek怎麼問他 deepseek怎麼問他 Feb 19, 2025 pm 04:42 PM

deepseek怎麼問他

evaluate函數怎麼保存 evaluate函數怎麼保存 May 07, 2024 am 01:09 AM

evaluate函數怎麼保存

See all articles