Python实现简单拆分PDF文件的方法
作者:willzhao 时间:2022-09-27 19:20:37
本文实例讲述了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程序设计有所帮助。
标签:Python,PDF
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python函数中4种参数的使用教程
2023-03-06 23:06:03
python学习字符串驻留与常量折叠隐藏特性详解
2021-01-19 14:40:10
![](https://img.aspxhome.com/file/2023/8/103488_0s.png)
从pandas一个单元格的字符串中提取字符串方式
2022-10-14 21:24:35
Python object类中的特殊方法代码讲解
2021-01-27 13:40:33
Python编程快速上手——疯狂填词程序实现方法分析
2023-07-25 08:57:17
![](https://img.aspxhome.com/file/2023/5/61475_0s.png)
Python面向对象程序设计之继承、多态原理与用法详解
2023-07-08 18:17:52
![](https://img.aspxhome.com/file/2023/6/82716_0s.png)
python办公自动化之excel的操作
2023-11-20 14:01:01
Python企业编码生成系统总体系统设计概述
2021-03-31 09:12:19
![](https://img.aspxhome.com/file/2023/9/97319_0s.png)
python如何将两张图片生成为全景图片
2021-08-03 23:42:26
![](https://img.aspxhome.com/file/2023/0/81480_0s.jpg)
Python3多线程处理爬虫的实战
2023-08-16 02:16:21
Pycharm最新激活码2019(推荐)
2023-07-24 20:30:26
![](https://img.aspxhome.com/file/2023/1/89221_0s.jpg)
很有意思的SQL多行数据拼接
2011-11-03 17:08:29
python关闭print输出信息详情
2023-06-21 17:21:09
python装饰器使用方法实例
2022-12-14 11:24:07
简单谈谈python中的语句和语法
2023-03-03 12:03:13
PyTorch 中的 torch.utils.data 解析(推荐)
2021-09-14 01:58:16
python函数的作用域及关键字详解
2021-11-26 04:38:30
Python实现的检测网站挂马程序
2023-11-21 16:39:38
解决python pandas读取excel中多个不同sheet表格存在的问题
2022-03-12 06:59:33
![](https://img.aspxhome.com/file/2023/2/110282_0s.jpg)
Python异步处理返回进度——使用Flask实现进度条
2023-10-19 23:23:08
![](https://img.aspxhome.com/file/2023/7/102947_0s.gif)