![]() ![]() Section 7.11.3 add_blank_page ( width : Optional = None, height : Optional = None ) → PyPDF2._page.PageObject Īppend a blank page to this PDF file and returns it. add_annotation ( page_number : int, annotation : Dict ) → None add_attachment ( filename : str, data : Union ) → None įilename ( str) – The filename to display. PYTHON PDFWRITER CODEWith just a few lines of Python code you can delete pages, separate them, and add new content.Deprecated since version 1.28.0: Use add_uri() instead. Re-writing and re-arranging the structure of a PDF is fairly easy with the libraries pdfrw, PyMuPDF, and PyPDF2. # Write the data to disk with open(output_filename_odd, "wb") as out: # Write the data to disk with open(output_filename_even, "wb") as out: # Get reach page and add it to corresponding # output file based on page number for page in range(pdf.getNumPages()): ![]() #!/usr/bin/python3 from PyPDF2 import PdfFileReader, PdfFileWriter At the end the two streams are saved to disk in separate files, as defined before. Pages with even page numbers are added to the stream pdf_writer_even using addPage(), and odd numbers are added to the stream pdf_writer_odd. Next, in a for-loop the script goes through the entire PDF file, and reads one page after the other. This Python script starts with the definition of two output files, even.pdf and odd.pdf, as well as their corresponding writer objects pdf_writer_even and pdf_writer_odd. The following example uses PyPDF2 and does this by taking a file, separating it into its even and odd pages, saving the even pages in the file even.pdf, and the odd pages in odd.pdf. Output_file_path = "example-extended.pdf" # !/usr/bin/python # Recall that PyMuPDF is imported as fitz import fitz PYTHON PDFWRITER HOW TOThe next example shows how to add a page from a different PDF document at the end of another one. It provides the methods newPage() for adding completely blank pages, and insertPage() in order to add an existing page. The PyMuPDF library allows you to insert pages as well. Output_file = "example-rearranged.pdf" # Define the pages to keep - 1, 2 and 4 In our case the output document contains the first, second, and fourth pages only. Be aware that the pages that are not specified will not be part of the output document. ![]() The following example will demonstrate how to use a list in order to select the pages to keep from the original document. It allows you to specify either a single page (using the deletePage() method), or a range of page numbers (using the deletePageRange() method), or a list with the page numbers (using the select() method). The PyMuPDF library comes with quite a few sophisticated methods that simplify deleting pages from a PDF file. Print( "adding page %i" % (current_page + 1)) Writer_output.addpage(reader_input.pages) # Go through the pages one after the next for current_page in range( len(reader_input.pages)): Output_file = "example-updated.pdf" # Define the reader and writer objects # !/usr/bin/python # Remove the first two pages (cover sheet) from the PDF from pdfrw import PdfReader, PdfWriter Except for both the first and second page, each page is added to the output file using the addpage() method, and then written to disk eventually.įigure 1 shows the output when executing the code on a four-page PDF file. ![]() Using the pdfrw library, the file is read with the help of the PdfReader() class first. The following example removes the first two pages from a PDF document.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |