栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现PDF合并

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现PDF合并

一、单个函数 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 
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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938742.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号