2

PDF 拆分/合并 - 未名w

 2 years ago
source link: https://www.cnblogs.com/weimingai/p/16536287.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

PDF 拆分/合并

不会真的有人会去下载那些广告免费,实则要收会员费的黑心软件来进行PDF的拆分合并吧???

在下载两个均不能免费实现PDF自由拆分、合并,以及PDF打印方式会增加文件大小的情况下,一个合格的程序员肯定不能向不良商家低头,所以使用PyPDF2实现这些功能,并给大家分享一个简单、易用、易懂的python小程序。

from PyPDF2 import PdfFileReader, PdfFileMerger, PdfFileWriter

def merge(input1,input2,output):

    file_merger = PdfFileMerger()
    file_merger.append(input1)
    file_merger.append(input2)
    file_merger.write(output)

def split(input,output,start_page, end_page):
    try:
        read_file = input
        fp_read_file = open(read_file, 'rb')
        pdf_input = PdfFileReader(fp_read_file)  # 将要分割的PDF内容格式话
        page_count = pdf_input.getNumPages()  # 获取PDF页数
        print("该文件共有{}页".format(page_count))  # 打印页数

        try:
            print(f'开始分割{start_page}页-{end_page}页,保存为{output}......')
            pdf_output = PdfFileWriter()  # 实例一个 PDF文件编写器
            for i in range(start_page, end_page):
                pdf_output.addPage(pdf_input.getPage(i))
            with open(output, 'wb') as sub_fp:
                pdf_output.write(sub_fp)
            print(f'完成分割{start_page}页-{end_page}页,保存为{output}!')
        except IndexError:
            print(f'分割页数超过了PDF的页数')
        # fp.close()
    except Exception as e:
        print(e)


if __name__ == '__main__':
    input1 = open(r"1.pdf", "rb") #打开第一个PDF文件
    input2 = open(r"2.pdf", "rb") #打开第二个PDF文件
    output=r'submit.pdf'
    merge(input1,input2,output)

    # input = r"submit.pdf"  #打开第二个PDF文件
    # output= r'1.pdf'
    # split(input,output,9,35) # start 起始页 从0 开始算,end 是尾页从1开始算

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK