python日常应用——pdf拆分和合并 python PyPDF2
#! /usr/bin/env python# -*- coding: utf-8 -*-from PyPDF2 import PdfFileWriter, PdfFileReader,PdfFileMergerdef pdf_split(pdf_in):"""拆分PDF成单页"""# 读取pdfwith open(pdf_in,'rb') as in_pdf:pdf_file = PdfFile
·
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from PyPDF2 import PdfFileWriter, PdfFileReader,PdfFileMerger
def pdf_split(pdf_in):
"""拆分PDF成单页"""
# 读取pdf
with open(pdf_in,'rb') as in_pdf:
pdf_file = PdfFileReader(in_pdf)
if pdf_file.isEncrypted:
pdf_file.decrypt('')
page_num = pdf_file.getNumPages()
# 从pdf中逐页存储
for i in range(page_num):
# 建立一个pdf
pdf_out = 'after_split_b_' + str(i) +'.pdf'
output = PdfFileWriter()
output.addPage(pdf_file.getPage(i))
# 写出pdf
with open(pdf_out,'ab') as out_pdf:
output.write(out_pdf)
if __name__ == '__main__':
"""拆分PDF"""
pdf_split("pdf_in")
"""合并PDF"""
# 初始化
merger = PdfFileMerger()
# 循环,a,b 交叉合并
for i in range(72):
with open('after_split_b_' + str(i) + '.pdf' , 'rb') as pdf:
merger.append(PdfFileReader(pdf))
with open('after_split_a_' + str(i+1) + '.pdf' , 'rb') as pdf:
merger.append(PdfFileReader(pdf))
# 合并单页
merger.append(PdfFileReader('after_split_a_73.pdf'))
merger.append(PdfFileReader('after_split_a_74.pdf'))
merger.write('merged.pdf')
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献5条内容
所有评论(0)