def pdf_insert(<EOL>dest: str,<EOL>source: str,<EOL>pages: [str] = None,<EOL>index: int = None,<EOL>output: str = None,<EOL>):
if output is not None and os.path.isfile(output):<EOL><INDENT>ans = input(<EOL>"<STR_LIT>"<EOL>"<STR_LIT>" % output<EOL>).lower()<EOL>if ans not in ["<STR_LIT:y>", "<STR_LIT>"]:<EOL><INDENT>return<EOL><DEDENT><DEDENT>writer = PdfFileWriter()<EOL>destfile = open(dest, "<STR_LIT:rb>")<EOL>destreader = PdfFileReader(destfile)<EOL>for page in destreader.pages:<EOL><INDENT>writer.addPage(page)<EOL><DEDENT>srcfile = open(source, "<STR_LIT:rb>")<EOL>srcreader = PdfFileReader(srcfile)<EOL>index = limit(index - <NUM_LIT:1>, <NUM_LIT:0>, len(destreader.pages))<EOL>if pages is None:<EOL><INDENT>for i, page in enumerate(srcreader.pages):<EOL><INDENT>if index is None:<EOL><INDENT>writer.addPage(page)<EOL><DEDENT>else:<EOL><INDENT>writer.insertPage(page, index + i)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>pages = parse_rangearg(pages, len(srcreader.pages))<EOL>for i, pagenr in enumerate(pages):<EOL><INDENT>page = srcreader.getPage(pagenr)<EOL>if index is None:<EOL><INDENT>writer.addPage(page)<EOL><DEDENT>else:<EOL><INDENT>writer.insertPage(page, index + i)<EOL><DEDENT><DEDENT><DEDENT>if output is None:<EOL><INDENT>ans = input(<EOL>"<STR_LIT>" % dest<EOL>).lower()<EOL>if ans in ["<STR_LIT:y>", "<STR_LIT>"]:<EOL><INDENT>tempfile = NamedTemporaryFile(delete=False)<EOL>writer.write(tempfile)<EOL>tempfile.close()<EOL>move(tempfile.name, dest)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>with open(output, "<STR_LIT:wb>") as outfile:<EOL><INDENT>writer.write(outfile)<EOL><DEDENT><DEDENT>destfile.close()<EOL>srcfile.close()<EOL>
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