python导出pdf文件怎么只有一页_python利用PyPDF2拆分pdf文件成单页

需要安装PyPDF2库,

pip3 install PyPDF2

代码如下:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

from PyPDF2 import PdfFileReader, PdfFileWriter

import os

def split_pdf(infile, out_path):

"""

:param infile: 待拆分的pdf文件

:param out_path: 拆分成单页的pdf文件的存储路径

:return: 无

"""

if not os.path.exists(out_path):

os.makedirs(out_path)

with open(infile, 'rb') as infile:

reader = PdfFileReader(infile)

number_of_pages = reader.getNumPages() #计算此PDF文件中的页数

for i in range(number_of_pages):

writer = PdfFileWriter()

writer.addPage(reader.getPage(i))

out_file_name = out_path + str(i+1)+'.pdf'

with open(out_file_name, 'wb') as outfile:

writer.write(outfile)

if __name__ == '__main__':

in_File = './Schedule.pdf'

out_Path = './PDF试验/单页的/' # 生成输出文件夹

split_pdf(in_File, out_Path)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值