600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > python导出pdf文件怎么只有一页_python利用PyPDF2拆分pdf文件成单页

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

时间:2019-05-16 01:16:42

相关推荐

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)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。