def pdf_add(dest: str, source: str, pages: [str], output: str):
if output is not None and os.path.isfile(output):<EOL><INDENT>if not overwrite_dlg(output):<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>if pages is None:<EOL><INDENT>for i, page in enumerate(srcreader.pages):<EOL><INDENT>writer.addPage(page)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>pages = parse_rangearg(pages, len(srcreader.pages))<EOL>for pagenr in pages:<EOL><INDENT>page = srcreader.getPage(pagenr)<EOL>writer.addPage(page)<EOL><DEDENT><DEDENT>if output is None:<EOL><INDENT>if overwrite_dlg(dest):<EOL><INDENT>tempfile = NamedTemporaryFile(delete=False)<EOL>writer.write(tempfile)<EOL>tempfile.close()<EOL>destfile.close()<EOL>srcfile.close()<EOL>os.remove(dest)<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>destfile.close()<EOL>srcfile.close()<EOL><DEDENT><DEDENT>
Add pages from a source pdf file to an output file. If the output
file does not exist a new file will be created.
:param source: source pdf file
:param dest: destination pdf file
:param pages: list of page numbers or range expressions
:param output: output pdf file