def pdf_insert(
    dest: str,
    source: str,
    pages: [str] = None,
    index: int = None,
    output: str = None,
):
    """
    Insert pages from one file into another.
    :param dest: Destination file
    :param source: Source file
    :param pages: list of page numbers to insert
    :param index: index in destination file where to insert the pages
    :param output: output file

    """
    if output is not None and os.path.isfile(output):
        ans = input(
            "The file '%s' already exists. "
            "Overwrite? Yes/Abort [Y/a]: " % output
        ).lower()
        if ans not in ["y", ""]:
            return

    writer = PdfFileWriter()
    # read pages from file1
    destfile = open(dest, "rb")
    destreader = PdfFileReader(destfile)
    for page in destreader.pages:
        writer.addPage(page)

    # read pages from file2
    srcfile = open(source, "rb")
    srcreader = PdfFileReader(srcfile)

    # if no page numbers are given insert all pages
    index = limit(index - 1, 0, len(destreader.pages))
    if pages is None:
        for i, page in enumerate(srcreader.pages):
            if index is None:
                writer.addPage(page)
            else:
                writer.insertPage(page, index + i)
    else:
        pages = parse_rangearg(pages, len(srcreader.pages))
        for i, pagenr in enumerate(pages):
            page = srcreader.getPage(pagenr)
            if index is None:
                writer.addPage(page)
            else:
                writer.insertPage(page, index + i)

    if output is None:
        # Write into Temporary File first and then overwrite dest file
        ans = input(
            "Overwrite the file '%s'? Yes/Abort [Y/a]: " % dest
        ).lower()
        if ans in ["y", ""]:
            tempfile = NamedTemporaryFile(delete=False)
            writer.write(tempfile)
            tempfile.close()
            move(tempfile.name, dest)
    else:
        with open(output, "wb") as outfile:
            writer.write(outfile)
    destfile.close()
    srcfile.close()
def pdf_insert ( dest : str , source : str , pages : [ str ] = None , index : int = None , output : str = None , ) : if output is not None and os . path . isfile ( output ) : ans = input ( "The file '%s' already exists. " "Overwrite? Yes/Abort [Y/a]: " % output ) . lower ( ) if ans not in [ "y" , "" ] : return writer = PdfFileWriter ( ) destfile = open ( dest , "rb" ) destreader = PdfFileReader ( destfile ) for page in destreader . pages : writer . addPage ( page ) srcfile = open ( source , "rb" ) srcreader = PdfFileReader ( srcfile ) index = limit ( index - 1 , 0 , len ( destreader . pages ) ) if pages is None : for i , page in enumerate ( srcreader . pages ) : if index is None : writer . addPage ( page ) else : writer . insertPage ( page , index + i ) else : pages = parse_rangearg ( pages , len ( srcreader . pages ) ) for i , pagenr in enumerate ( pages ) : page = srcreader . getPage ( pagenr ) if index is None : writer . addPage ( page ) else : writer . insertPage ( page , index + i ) if output is None : ans = input ( "Overwrite the file '%s'? Yes/Abort [Y/a]: " % dest ) . lower ( ) if ans in [ "y" , "" ] : tempfile = NamedTemporaryFile ( delete = False ) writer . write ( tempfile ) tempfile . close ( ) move ( tempfile . name , dest ) else : with open ( output , "wb" ) as outfile : writer . write ( outfile ) destfile . close ( ) srcfile . close ( )
Insert pages from one file into another.
    :param dest: Destination file
    :param source: Source file
    :param pages: list of page numbers to insert
    :param index: index in destination file where to insert the pages
    :param output: output file