Python实现PDF合并

本文介绍了Python中的三个实用函数:str_to_int用于字符串转整数,pdf_file_list遍历指定路径查找PDF文件,以及pdf_merge实现PDF文件的合并。通过实例展示了如何使用这些功能处理文件操作任务。
摘要由CSDN通过智能技术生成

一、单个函数

1、str_to_int()函数:将input()输入的字符串转化为int

def str_to_int(str):
    '''
    Convert string to int

    param:: "str": string

    return:: "x":int
    '''
    str1 = re.sub(' ','',str)
    print(str1)
    assert str1.isdigit(),"Please input the current index!"
    x = []
    slen = len(str)
    i = 0
    s = 0
    f = 0 
    while i<slen:
        if str[i] != " ":
            f = 1
            s = s*10+int(str[i])
        elif str[i] ==" ":
            if f == 1:
               x.append(s)
               s = 0
        i = i+1
    if s!=0:
        if f==1:
            x.append(s)
    return x

2 、pdf_file_list()函数:pdf文件搜索函数

def pdf_file_list(fpath):
    """
    Get paths of all pdf files


    param:: "fpath":The root path

    return:: "pdf_paths":The paths of all pdf files
    """
    assert os.path.exists(fpath), 'The folder path does not exist'
    pdf_paths = list()
    for root, dirs, files in os.walk(fpath):
        for file in files:
            if file.endswith('.pdf'):
                pdf_paths.append(os.path.join(root, file))
    return pdf_paths

3、pdf_merge()函数:pdf拼接函数

def pdf_merge(fpath):
    """
    Merge the pdf files

    param:: "fpath":The paths of pdf

    return:: None 
    """
    fmerge = PyPDF2.PdfFileMerger()
    for pdf in fpath:
        fmerge.append(pdf)
    fmerge.write('result.pdf')

二、全部代码

from importlib.resources import path
from operator import length_hint
import re
import PyPDF2 
import os
from numpy import append
from requests import patch
 
def pdf_file_list(fpath):
    """
    Get paths of all pdf files


    param:: "fpath":The root path

    return:: "pdf_paths":The paths of all pdf files
    """
    assert os.path.exists(fpath), 'The folder path does not exist'
    pdf_paths = list()
    for root, dirs, files in os.walk(fpath):
        for file in files:
            if file.endswith('.pdf'):
                pdf_paths.append(os.path.join(root, file))
    return pdf_paths

def pdf_merge(fpath):
    """
    Merge the pdf files

    param:: "fpath":The paths of pdf

    return:: None 
    """
    fmerge = PyPDF2.PdfFileMerger()
    for pdf in fpath:
        fmerge.append(pdf)
    fmerge.write('result.pdf')

def str_to_int(str):
    '''
    Convert string to int

    param:: "str": string

    return:: "x":int
    '''
    str1 = re.sub(' ','',str)
    print(str1)
    assert str1.isdigit(),"Please input the current index!"
    x = []
    slen = len(str)
    i = 0
    s = 0
    f = 0 
    while i<slen:
        if str[i] != " ":
            f = 1
            s = s*10+int(str[i])
        elif str[i] ==" ":
            if f == 1:
               x.append(s)
               s = 0
        i = i+1
    if s!=0:
        if f==1:
            x.append(s)
    return x

def pdf_choose(fpath):
    '''
    Choose the merged pdf 


    param:: "fpath" :The paths of pdf
    return:: "pdf_path":The paths of merged pdf
    '''
    for i in range(len(fpath)):
        print(i,fpath[i])
    print("Please input the index of merged pdf!")
    x = []
    pdf_path = []
    x = input()
    xi = str_to_int(x)
    print(xi)
    xlen = len(xi)
    flen = len(fpath)
    for i in range(0,xlen):
        print(int(xi[i]),flen)
        assert (int(xi[i]) < flen),"Please input the current index!"
    for i in range(0,xlen):
        pdf_path.append(fpath[xi[i]])
    return pdf_path

fpath = pdf_file_list('D:')
fpath = pdf_choose(fpath)
pdf_merge(fpath)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值