Pypdf2.Pdffilewriter Python Example
Pypdf2.Pdffilewriter Python Example
# Utils
Example 3
                                                   pdf_writer.write(pdf_writer_fp)
                                                   click.echo("Deleted pages %s from %s and created new PDF at %s" % (delete_pages, file_arg, out))
Example 4
1 of 24                                                                                                                                                                                      7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                 https://www.programcreek.com/python/example/...
                               for i in range(num_pages):
                                     page = pdf_reader.getPage(i)
                                     if direction == "clockwise":
                                         page = page.rotateClockwise(90)
                                   else:
                                       page = page.rotateCounterClockwise(90)
                                   pdf_writer.addPage(page)
                               pdf_writer.write(pdf_writer_fp)
                               click.echo("Pages were rotated %s successfully and saved at %s" % (direction, out))
Example 5
Example 6
                     def edit_and_save_pdf(self):
                               """Return file object."""
                               original_pdf = PdfFileReader(self.original_pdf)
                               output = PdfFileWriter()
                               config_var_map = dict(
                                   (config['page_number'], config['variables'])
                                   for config in self.configuration
                               )
return output
Example 7
                                           if not pdfReader.isEncrypted:
                                               pdfWriter = PyPDF2. PdfFileWriter()
                                               for pageNum in range(pdfReader.numPages):
                                                     pdfWriter.addPage(pdfReader.getPage(pageNum))
                                                 pdfWriter.encrypt(password)
                                                 newPath = os.path.dirname(filepath) + '/untitled folder/' + \
                                                           ('_encrypted.'.join(os.path.basename(filepath).split('.')))
                                                 resultPdf = open(newPath, 'wb')
                                                 pdfWriter.write(resultPdf)
                                                 resultPdf.close()
Example 8
2 of 24                                                                                                                                             7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                       https://www.programcreek.com/python/example/...
                         A value of 0 for from_page will mean beginning of the document, and a value
                         of 0 for to_page will mean the end of the document
                         """
                         output = PdfFileWriter()
                         input1 = PdfFileReader(open(document, "rb"))
                         if from_page == 0:
                             from_page = 1
                         if to_page == 0:
                               to_page = input1.getNumPages()
Example 9
                               while listchar:
                                    iopage = self.outputtemplateonepage(listchar)
                                    page = PyPDF2.PdfFileReader(iopage)
                                    output.addPage(page.getPage(0))
                               if dest != None:
                                   if isinstance(dest, str): # when dest is a file path
                                          destdir = os.path.dirname(dest)
                                          if destdir != '' and not os.path.isdir(destdir):
                                              os.makedirs(destdir)
                                        with open(dest, "wb") as w:
                                            output.write(w)
                                    else: # when dest is io.IOBase
                                        output.write(dest)
                               else:
                                   return output
Example 10
                         # Intermediate file
                         with open(INTERMEDIATE_ENCRYPT_FILE, "wb") as out_file:
                             output_pdf.write(out_file)
in_file.close()
Example 11
                               Args:
                                   digits (str): Regex target digit.
                                   extension (str): Target file extension.
                                   directory_path (str): Target directory path.
                               """
                               super().__init__()
                               self.__digits = digits
                               self.__extension = self._convert_extension_with_dot(extension)
                               self.__regex_ext = re.compile(self.__extension)
                               self.__file_writer = PyPDF2. PdfFileWriter()
                               if directory_path is not None:
                                   self.__directory_path = directory_path
                               else:
                                   self.__directory_path = os.getcwd()
                                   logger.debug("Current Directory: {cwd}".format(cwd=self.__directory_path))
                               os.chdir(self.__directory_path)
Example 12
                     def pdf_cropper(source_path,destination_path,pdf_filename,number_page):
                         page_seperated=[]
                         PDFfilename = source_path #filename of your PDF/directory where your PDF is stored
                         pfr = PyPDF2.PdfFileReader(open(PDFfilename, "rb")) #PdfFileReader object
                         if pfr.isEncrypted: #needed for some encrypted files like AD7183
3 of 24                                                                                                                                                   7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                             https://www.programcreek.com/python/example/...
                              pfr.decrypt('')
                          #page_number= pfr.getNumPages()
                          for i in range(0,number_page):
                               #page_seperated.append(str(i))
                               if not os.path.exists(destination_path):
                                   os.makedirs(destination_path)
                               pdfname = pdf_filename.split(".pdf")[0]
                               page_name = pdfname + "_" + str(i) + ".pdf"
                               NewPDFfilename=os.path.join(destination_path,page_name)#filename of your PDF/directory where you want your new PDF to be
                               pg = pfr.getPage(i) #extract pg 1
                               writer = PyPDF2. PdfFileWriter() #create PdfFileWriter object
                               #add pages
                               writer.addPage(pg)
                               with open(NewPDFfilename, "wb") as outputStream: #create new PDF
                                   writer.write(outputStream) #write pages to new PDF
Example 13
                     def pdf_cropper(source_path,destination_path,pdf_filename,number_page):
                         page_seperated=[]
                          PDFfilename = source_path #filename of your PDF/directory where your PDF is stored
                          pfr = PyPDF2.PdfFileReader(open(PDFfilename, "rb")) #PdfFileReader object
                          if pfr.isEncrypted: #needed for some encrypted files lke AD7183
                              pfr.decrypt('')
                          #page_number= pfr.getNumPages()
                          for i in range(0,number_page):
                               page_seperated.append(str(i))
                               new_folder_path=os.path.join(destination_path, page_seperated[i])
                               if not os.path.exists(new_folder_path):
                                   os.makedirs(new_folder_path)
                               NewPDFfilename=os.path.join(new_folder_path,pdf_filename)#filename of your PDF/directory where you want your new PDF to be
                               pg = pfr.getPage(i) #extract pg 1
                               writer = PyPDF2. PdfFileWriter() #create PdfFileWriter object
                               #add pages
                               writer.addPage(pg)
                               with open(NewPDFfilename, "wb") as outputStream: #create new PDF
                                   writer.write(outputStream) #write pages to new PDF
Example 14
Project: pyzottk Author: sbrisard File: pdf.py BSD 3-Clause "New" or "Revised" License 6 votes
                          Args:
                               src (PyPDF2.PdfFileReader): The source.
                               dest (PyPDF2. PdfFileWriter): The destination.
                               outlines (list of PyPDF2.generic.Destination): The outlines to be
                                     copied (for recursive calls). If None, then uses all elements
                                   returned by``src.getOutlines()``.
                               parent (PyPDF2.generic.IndirectObject): The parent bookmark (if
                                   outlines are nested).
                          """
                          if outlines is None:
                              outlines = src.getOutlines()
                          for current, next in itertools.zip_longest(outlines, outlines[1:]):
                               if is_destination(current):
                                   bookmark = dest.addBookmark(current.title,
                                                               src.getDestinationPageNumber(current),
                                                                 parent=parent)
                                     if next and not is_destination(next):
                                           copy_bookmarks(src, dest, outlines=next, parent=bookmark)
Example 15
Project: pyzottk Author: sbrisard File: pdf.py BSD 3-Clause "New" or "Revised" License 6 votes
                          Args:
                              istream: The input PDF (string or stream in 'rb' mode).
                              ostream: The output PDF (string or stream in 'wb' mode).
                              author: The '/Author' metadata (string).
                              title: The '/Title' metadata (string).
                          """
                          reader = PyPDF2.PdfFileReader(istream)
                          writer = PyPDF2. PdfFileWriter()
                          writer.appendPagesFromReader(reader)
                          writer.addMetadata({'/Author': author,
                                               '/Title': title})
                          copy_bookmarks(reader, writer)
                          writer.write(ostream)
Example 16
Project: knowledge-repo Author: airbnb File: image.py Apache License 2.0 6 votes
                          check_dependencies(__optional_dependencies__['pdf'])
                          # Import libraries within this function so as to avoid import-time dependence
                          import PyPDF2
4 of 24                                                                                                                                                     7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                               https://www.programcreek.com/python/example/...
from wand.image import Image # TODO: When we start using this again, document which system-level libraries are required.
                          pdf_bytes = io.BytesIO()
                          dst_pdf.write(pdf_bytes)
                          pdf_bytes.seek(0)
return img
Example 17
Example 18
Project: callisto-core Author: project-callisto File: test_pdf.py GNU Affero General Public License v3.0 6 votes
                     def test_output_file(self):
                                """
                                      for when you want to see what the file looks like
                                      $ open MatchingUserReviewPDFTest.pdf
                                """
                                matching_id = "test1a08daw awd7awgd 1213123"
                                self.create_match(self.user1, matching_id)
                                self.create_match(self.user2, matching_id)
                                self.most_recent_report.contact_phone = "555-555-5555"
                                self.most_recent_report.save()
                                pdf = PDFUserReviewReport.generate({"matches": MatchReport.objects.all()})
                                pdf_reader = PyPDF2.PdfFileReader(BytesIO(pdf))
                                with open("MatchingUserReviewPDFTest.pdf", "wb") as _file:
                                    dst_pdf = PyPDF2. PdfFileWriter()
                                      dst_pdf.appendPagesFromReader(pdf_reader)
                                      dst_pdf.write(_file)
Example 19
Example 20
Project: pythonlib Author: RudolfCardinal File: pdf.py Apache License 2.0 6 votes
                          Args:
                              input_pdf: the PDF, as ``bytes``
                              writer: the writer
                              start_recto: start a new right-hand page?
                          """
                          if not input_pdf:
                              return
5 of 24                                                                                                                                                             7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                      https://www.programcreek.com/python/example/...
Example 21
Project: pythonlib Author: RudolfCardinal File: pdf.py Apache License 2.0 6 votes
                     def get_concatenated_pdf_in_memory(
                               pdf_plans: Iterable[PdfPlan],
                               start_recto: bool = True) -> bytes:
                          """
                          Concatenates PDFs and returns them as an in-memory binary PDF.
                          Args:
                              pdf_plans: iterable of :class:`PdfPlan` objects
                               start_recto: start a new right-hand page for each new PDF?
                          Returns:
                               concatenated PDF, as ``bytes``
                          """
                          writer = PdfFileWriter()
                          for pdfplan in pdf_plans:
                              pdfplan.add_to_writer(writer, start_recto=start_recto)
                          return pdf_from_writer(writer)
                     # =============================================================================
                     # Main -- to enable logging for imports, for debugging
                     # =============================================================================
Example 22
Example 23
pages = [[0]]
                               page1 = ""
                               page1 += pdf.getPage(x).extractText() + "\n"
                               page2 = ""
                               page2 += pdf.getPage(x + 1).extractText() + "\n"
                               output.write(outputStream)
                               outputStream.close()
6 of 24                                                                                                                                  7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                       https://www.programcreek.com/python/example/...
Example 24
Project: pdf2imgpdf Author: felinx File: pdf2imgpdf.py Apache License 2.0 5 votes
                            pdf_bytes = io.BytesIO()
                            dst_pdf.write(pdf_bytes)
                            pdf_bytes.seek(0)
Example 25
                     Project: kissmanga-downloader Author: Astrames File: pdfMaker.py       GNU General Public License
                                                                                                                           5 votes
                     v3.0
                                if outputPDFName is None:
                                          outputPDFName = str(folder_with_pdfs) + ".pdf"
mypath = folder_with_pdfs
outputPDFName = outputPDFName.replace('.pdf',included_pdf+'.pdf')
Example 26
                               Args:
                                   filename (str): Path where the PDF should be saved.
                                   overwrite (bool): Switch to allow overwriting of existing files.
                               Returns:
                                   str: Path where the file was saved.
                               Raises:
                                   FileExistsError: When the file already exists and overwrite is not enabled.
                               """
                               filename = filename or "{begin}.pdf".format(begin=self.begin)
Example 27
                     def encrypt_pdf(password):
                         writer = PyPDF2. PdfFileWriter()
                         writer.encrypt(password)
                         with open(file=join(root, 'test_pdf.pdf'), mode='wb') as pdf_file:
                             writer.write(pdf_file)
Example 28
                     def copy_pdf_pages(pdf_reader):
                         """
                         Copies all pages from a given pdf reader object to a pdf writer object
7 of 24                                                                                                                                                   7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                          https://www.programcreek.com/python/example/...
return pdf_writer
Example 29
                          output = PdfFileWriter()
                          pages = range(flattened.getNumPages())
                          for p in pages:
                                output.addPage(flattened.getPage(p))
return output
Example 30
                                      self.context = ctx
                                      kwargs = self.template_args(data)
                                      template = self.context["template"]
                                      try:
                                          rendered_field = template.render(**kwargs)
                                      except Exception as err:
                                            logger.error("%s: %s %s", field, template, err)
                                      else:
                                          # Skip the field if it is already rendered by filter
                                          if field not in self.rendered:
                                                  if PY3:
                                                      field = field.decode('utf-8')
                                                  self.rendered[field] = rendered_field
                                filled = PdfFileReader(self.exec_pdftk({}))
                                for pagenumber, watermark in self.watermarks:
                                      page = filled.getPage(pagenumber)
                                      page.mergePage(watermark)
                                output = PdfFileWriter()
                                pages = pages or xrange(filled.getNumPages())
                                for p in pages:
                                    output.addPage(filled.getPage(p))
return output
Example 31
                     def interleave(self):
                                     self.input_path = self.entry_in.get()
                                     self.output_path = self.entry_out.get()
Example 32
                     def test_pypdf2_writer(self):
                             reader_pdf1 = PyPDF2.PdfFileReader(self.test_pdf_file_handles[0])
                                fp = tempfile.TemporaryFile()
                                writer.write(fp)
8 of 24                                                                                                                                      7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                     https://www.programcreek.com/python/example/...
                               fp.seek(0)
                               pdf_reader = PyPDF2.PdfFileReader(fp)
self.assertEqual(pdf_reader.getNumPages(), 1)
Example 33
                          if order:
                               try:
                                      order = order.split(",")
                                      order = [int(num) for num in order]
                               except ValueError as e:
                                   raise click.BadParameter("order must be a list of integers representing indexes in PDF.")
                               if order:
                                   for index in order:
                                       if index > num_pages - 1:
                                                 raise click.BadParameter('Indexes start from zero must be less than the number of pages')
                               if reverse:
                                      order = [i for i in range(num_pages - 1, -1, -1)]
                               pdf_writer.write(pdf_fp_w)
                               click.echo("Reordered pages in %s and rewrote file to %s" % (file_arg, out))
Example 34
num_pages = pdf_reader.getNumPages()
                               for i in range(num_pages):
                                   if i < split_index:
                                       pdf_writer_one.addPage(pdf_reader.getPage(i))
                                   else:
                                       pdf_writer_two.addPage(pdf_reader.getPage(i))
                                   pdf_writer_one.write(pdf_fp_one)
                                   pdf_writer_two.write(pdf_fp_two)
                               click.echo("Split %s at index %s into %s and %s" % (file_arg, split_index, out_first, out_second))
Example 35
Example 36
Project: pdfdir Author: chroming File: api.py GNU General Public License v3.0 5 votes
9 of 24                                                                                                                                      7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                    https://www.programcreek.com/python/example/...
                               self.writer.appendPagesFromReader(reader)
                               self.writer.addMetadata({k: v for k, v in reader.getDocumentInfo().items()
                                                                   if isinstance(v, (utils.string_type, utils.bytes_type))})
Example 37
                     def convertor(self):
                             """
                               Convert all given images into pdf file with stream way.
                               """
                               merge_img2pdf = PdfFileWriter()
                               for img_pdf in self.img_pdf_file:
                                   merge_tmp = PdfFileReader(open(img_pdf, 'rb'))
                                   num_pages = merge_tmp.getNumPages()
                                   for i in range(num_pages):
                                       merge_img2pdf.addPage(merge_tmp.getPage(i))
                                   os.remove(img_pdf)
                               with open(self.out_pdf_name, 'wb') as outputstream:
                                     merge_img2pdf.write(outputstream)
Example 38
Example 39
Example 40
Project: csdn Author: spygg File: csdn.py GNU General Public License v3.0 5 votes
                               self.username = username
                               self.baseUrl = 'https://blog.csdn.net/%s/article/list/' % username;
                               self.conn = sqlite3.connect('csdn.db')
                               self.cursor = self.conn.cursor()
                               self.cursor.execute('''
                                   CREATE TABLE IF NOT EXISTS %s(
                                   id         INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                                   url        TEXT,
                                     title      TEXT,
                                     srcHtml    BLOB
                                     )
                                     ''' % self.username)
                               self.cursor.execute('''
                                   CREATE TABLE IF NOT EXISTS %s_Index(
                                   id         INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
                                   url        TEXT,
                                   indexHtml    BLOB
                                   )
                                     ''' % self.username)
                               self.articleNumber = 0
                               self.merge = PdfFileWriter()
                               self.catlogPageNum = 0
Example 41
Project: opencanary Author: thinkst File: testpdf.py BSD 3-Clause "New" or "Revised" License 5 votes
if size == '10kb':
10 of 24                                                                                                                                               7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                 https://www.programcreek.com/python/example/...
                                    randlength = random.randint(10000,90000)
                                elif size == '100kb':
                                     randlength = random.randint(100000,900000)
                                elif size == '1mb':
                                    randlength = random.randint(1000000,9000000)
                                #create file
                                pdf=FPDF()
                                pdf.add_page()
                                pdf.set_font('Arial','B',8)
                                pdf.cell(0,0,os.urandom(randlength))
                                pdf.output(tmp_name, "F")
                                #encrypt it
                                output = PdfFileWriter()
                                input1 = PdfFileReader(open(tmp_name, "rb"))
                                output.encrypt(user_pwd="ihasapass")
                                output.addPage(input1.getPage(0))
Example 42
Project: dir2pdf Author: aliagdeniz File: dir2pdf.py GNU General Public License v3.0 5 votes
                     def add_bookmark(list1,list2):
                             output = PdfFileWriter()
                                input1 = PdfFileReader(open('output.pdf', 'rb'))
                                num = input1.getNumPages()
                                for i in range(0,num):
                                           output.addPage(input1.getPage(i))
                                for i in range(0,len(list1)):
                                           output.addBookmark(list1[i], list2[i])
                                os.remove("output.pdf")
                                pdf = open("output.pdf", "wb")
                                output.write(pdf)
Example 43
                                           if pdfReader.isEncrypted:
                                               success = pdfReader.decrypt(password)
                                                 if success:
                                                     pdfWriter = PyPDF2. PdfFileWriter()
                                                       for pageNum in range(pdfReader.numPages):
                                                           pdfWriter.addPage(pdfReader.getPage(pageNum))
                                                       newPath = os.path.dirname(filepath) + '/' + \
                                                             ''.join(os.path.basename(filepath).split('_encrypted'))
                                                     resultPdf = open(newPath, 'wb')
                                                     pdfWriter.write(resultPdf)
                                                     resultPdf.close()
                                                 else:
                                                       print('wrong password provided')
Example 44
Project: ExtractTable-py Author: ExtractTable File: __init__.py Apache License 2.0 5 votes
Example 45
11 of 24                                                                                                                                            7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                        https://www.programcreek.com/python/example/...
pdf_writer.write(outfile)
Example 46
                         # Generate each of the pages: a full summary, graph by day, and by shop
                         total_summary = generate_summary(data)
                         products = total_summary['by_product'].keys()
                         summary_by_day = aggregate_by_day(data)
                         summary_by_shop = aggregate_by_shop(data)
                         # Compose the PDF with a brief summary and all the graphs
                         summary_file = create_summary_brief(total_summary, 'summary.pdf')
                         by_day_file = graph(summary_by_day, products, 'by_day.pdf', 7)
                         by_shop_file = graph(summary_by_shop, products, 'by_shop.pdf')
Example 47
Project: pythonpdf Author: iprapas File: stamp.py GNU General Public License v3.0 5 votes
                         for i in xrange(pdf_in.getNumPages()):
                             page = pdf_in.getPage(i)
                             page.mergePage(stamp)
                             output.addPage(page)
Example 48
                     def pdf_cropper_all(source_path,destination_path,pdf_filename,number_page):
                         ## generate number_page pdf in one file
                         PDFfilename = source_path #filename of your PDF/directory where your PDF is stored
                         pfr = PyPDF2.PdfFileReader(open(PDFfilename, "rb")) #PdfFileReader object
                         if pfr.isEncrypted: #needed for some encrypted files lke AD7183
                             pfr.decrypt('')
                         NewPDFfilename = os.path.join(destination_path, pdf_filename) # filename of your PDF/directory where you want your new PDF to be
                         writer = PyPDF2. PdfFileWriter() # create PdfFileWriter object
                         for i in range(0, number_page):
                             pg = pfr.getPage(i) # extract pg 1
                             # add pages
                             writer.addPage(pg)
                         with open(NewPDFfilename, "ab+") as outputStream: # create new PDF
                             writer.write(outputStream) # write pages to new PDF
Example 49
Project: gallipy Author: GeoHistoricalData File: getpdf.py GNU Affero General Public License v3.0 5 votes
Example 50
def _run_ocr(self):
12 of 24                                                                                                                                                    7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                    https://www.programcreek.com/python/example/...
                               if self.pdf is None:
                                   return
                               pdf_pages = list()
                               for page in self.pdf.pages:
                                   image = page.to_image(resolution=100)
                                    pdf = pytesseract.image_to_pdf_or_hocr(image.original, extension='pdf')
                                    pdf_pages.append(pdf)
                               dirname = os.path.dirname(self.paths[self.pathidx])
                               filename = os.path.basename(self.paths[self.pathidx])
                               self.paths[self.pathidx] = path
                               self._load_file()
Example 51
                     Project: smart-manuscript Author: antemons File: searchable_pdf.py   GNU General Public License
                                                                                                                        5 votes
                     v3.0
                               transcription_pdf = PdfFileReader(layer)
                               original_pdf = PdfFileReader(open(input_pdf, 'rb'))
                               page = original_pdf.getPage(0)
                               page.mergePage(transcription_pdf.getPage(0))
                               output = PdfFileWriter()
                               output.addPage(page)
Example 52
Project: pdf-hacks Author: AnnaMag File: pdf_processing.py BSD 2-Clause "Simplified" License 5 votes
                               res = []
                               if not end_page:
                                    end_page = inputpdf.numPages
for i in range(len(list_pages)):
output_i = PdfFileWriter()
                                    if i == len(list_pages)-1:
                                        end = end_page -1
                                    else:
                                          end = list_pages[i+1] -1
                                    sio = BytesIO()
                                    output_i.write(sio)
res.append(sio)
return res
Example 53
                     def Run(self):
                                          self.__messages()
                                          allFiles = os.listdir(self.__ROOTPATH)
                                          CDMF.print_blue_text("扫描待统计村�资料...,")
                                          nNumFile = 0;
                                          nNumNoContent = 0;
                                          for fileOrDir in allFiles:
                                                  if fileOrDir.startswith(('1','2','3','4','5','6','7','8','9','0')) and fileOrDir.endswith('.pdf'):
                                                          nNumFile = nNumFile + 1
                                          CDMF.print_blue_text("扫描�毕�共有 "+str(nNumFile) + " 户的资料,",end='')
                                          CDMF.print_blue_text("需�统计的有 "+str(nNumFile) + " 户.")
                                          #��村
                                          bdeleteOrg = self.__quiry("是否删掉��件(请输�y或n):")
                                          index = 1
                                          for file in allFiles:
                                                  filefull = os.path.join(self.__ROOTPATH,file)
                                                  if not os.path.isdir(filefull):
                                                          if filefull.endswith('.pdf'):         #找��.pdf结尾的�件
                                                                  (filepath,tempfilename) = os.path.split(filefull)
                                                                  (filename,extension) = os.path.splitext(tempfilename)
                                                                  if filename.startswith(('1','2','3','4','5','6','7','8','9','0')):
13 of 24                                                                                                                                               7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                               https://www.programcreek.com/python/example/...
Example 54
Project: deda Author: dfd-tud File: privacy.py GNU General Public License v3.0 5 votes
Example 55
Project: deda Author: dfd-tud File: privacy.py GNU General Public License v3.0 5 votes
                                Example:
                                      def func(w,h): ...
                                      with open("output.pdf","wb") as fp_out:
                                          with open("input.pdf","rb") as fp_in:
                                              fp_out.write(pdfWatermark(pf_in.read(), func))
                                """
                                output = PdfFileWriter()
                                if not isinstance(pdfin, bytes):
                                    with open(pdfin,"rb") as fp: pdfin = fp.read()
                                input_ = PdfFileReader(BytesIO(pdfin))
                                outIO = BytesIO()
                                output.write(outIO)
                                outIO.seek(0)
                                return outIO.read()
Example 56
Example 57
Project: callisto-core Author: project-callisto File: test_pdf.py GNU Affero General Public License v3.0 5 votes
                     def test_output_file(self):
                             """
                                 for when you want to see what the file looks like
                                 $ open UserReviewPDFTest.pdf
                             """
                             self.client_post_report_creation()
                             self.client_post_report_prep()
14 of 24                                                                                                                                                                    7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                             https://www.programcreek.com/python/example/...
Example 58
                                     self.context = ctx
                                     kwargs = self.template_args(data)
                                     template = self.context["template"]
                                     try:
                                           rendered_field = template.render(**kwargs)
                                     except Exception as err:
                                         logger.error("%s: %s %s", field, template, err)
                                     else:
                                         # Skip the field if it is already rendered by filter
                                            if field not in self.rendered:
                                                if PY3:
                                                     field = field.decode('utf-8')
                                                 self.rendered[field] = rendered_field
                               filled = PdfFileReader(self.exec_pdftk(self.rendered))
                               for pagenumber, watermark in self.watermarks:
                                   page = filled.getPage(pagenumber)
                                     page.mergePage(watermark)
                               output = PdfFileWriter()
                               pages = pages or range(filled.getNumPages())
                               for p in pages:
                                   output.addPage(filled.getPage(p))
return output
Example 59
Project: txffpAssistant Author: huimingz File: pdf.py GNU General Public License v3.0 5 votes
                     def __init__(self):
                               self._pdf_writer = PdfFileWriter()
Example 60
Project: pythonlib Author: RudolfCardinal File: pdf.py Apache License 2.0 5 votes
                     def add_to_writer(self,
                                           writer: PdfFileWriter,
                                                   start_recto: bool = True) -> None:
                               """
                               Add the PDF described by this class to a PDF writer.
                               Args:
                                     writer: a :class:`PyPDF2. PdfFileWriter`
                                     start_recto: start a new right-hand page?
                               """
                               if self.is_html:
                                   pdf = get_pdf_from_html(
                                       html=self.html,
                                       header_html=self.header_html,
                                       footer_html=self.footer_html,
                                       wkhtmltopdf_filename=self.wkhtmltopdf_filename,
                                       wkhtmltopdf_options=self.wkhtmltopdf_options)
                                   append_memory_pdf_to_writer(pdf, writer, start_recto=start_recto)
                               elif self.is_filename:
                                   if start_recto and writer.getNumPages() % 2 != 0:
                                       writer.addBlankPage()
                                   writer.appendPagesFromReader(PdfFileReader(
                                       open(self.filename, 'rb')))
                               else:
                                   raise AssertionError("PdfPlan: shouldn't get here!")
                     # =============================================================================
                     # Ancillary functions for PDFs
                     # =============================================================================
Example 61
Project: pythonlib Author: RudolfCardinal File: pdf.py Apache License 2.0 5 votes
15 of 24                                                                                                                                        7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                  https://www.programcreek.com/python/example/...
Example 62
Project: pythonlib Author: RudolfCardinal File: pdf.py Apache License 2.0 5 votes
Example 63
Project: pythonlib Author: RudolfCardinal File: pdf.py Apache License 2.0 5 votes
                     # =============================================================================
                     # Serve concatenated PDFs
                     # =============================================================================
                     # Two ways in principle to do this:
                     # (1) Load data from each PDF into memory; concatenate; serve the result.
                     # (2) With each PDF on disk, create a temporary file (e.g. with pdftk),
                     #        serve the result (e.g. in one go), then delete the temporary file.
                     #        This may be more memory-efficient.
                     #        However, there can be problems:
                     #          http://stackoverflow.com/questions/7543452/how-to-launch-a-pdftk-subprocess-while-in-wsgi   # noqa
                     # Others' examples:
                     #   https://gist.github.com/zyegfryed/918403
                     #    https://gist.github.com/grantmcconnaughey/ce90a689050c07c61c96
                     #    http://stackoverflow.com/questions/3582414/removing-tmp-file-after-return-httpresponse-in-django     # noqa
Example 64
Project: python-drafthorse Author: pretix File: pdf.py Apache License 2.0 5 votes
                          reader = PdfFileReader(BytesIO(original_pdf))
                          output = PdfFileWriter()
                          # for page in reader.pages:
                          #      output.addPage(page)
                          output._header = "%PDF-1.6\r\n%\xc7\xec\x8f\xa2".encode()
                          output.appendPagesFromReader(reader)
                          original_pdf_id = reader.trailer.get('/ID')
                          if original_pdf_id:
                              output._ID = original_pdf_id
                                # else : generate some ?
                          _facturx_update_metadata_add_attachment(
                              output, xml_data, {}, level,
                              output_intents=_get_original_output_intents(reader),
                          )
                          outbuffer = BytesIO()
                          output.write(outbuffer)
                          outbuffer.seek(0)
                          return outbuffer.read()
Example 65
                     def blank_pdf(filepath):
                         """Add blank pages to PDF
                         """
                         logger.info("Adding blank pages")
                         input_pdf = PyPDF2.PdfFileReader(filepath)
                         output_pdf = PyPDF2. PdfFileWriter()
                         for page in input_pdf.pages:
                             output_pdf.addPage(page)
                             output_pdf.addBlankPage()
Example 66
16 of 24                                                                                                                                7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                https://www.programcreek.com/python/example/...
                     def __init__(
                             self, input_file=None, output_file=None, pdfcrop_path="pdfcrop"
                         ):
                               if not input_file is None:
                                   self.input_file = os.path.abspath(input_file)
                                   self.reader = PyPDF2.PdfFileReader(self.input_file)
                               if not output_file is None:
                                   self.output_file = os.path.abspath(output_file)
                               self.pdfcrop_path = pdfcrop_path
Example 67
Example 68
                               page = pdf_reader.getPage(page)
                               #�历mark⽬录下的⽣�的���件
                               watermark_obj=PdfFileReader('mark/'+str(a)+'.pdf')
                               watermark_page=watermark_obj.getPage(0)
                               page.mergePage(watermark_page)
                               pdf_writer.addPage(page)
                               a += 1
                               #print(pdf_reader.getNumPages())
                               #获��成的页�
                               rate = float(a)/float(pdf_reader.getNumPages())
                               #输��成的进�
                               print('File conversion completed :'+'%.2f%%' % (rate * 100)+'.')
#break
Example 69
                     def insertPage():
                         inserterWindow = tk.Tk()
                         inserterWindow.title("PDF page inserter")
tk.Label(inserterWindow, text="Inserts a single page inside an existing PDF").grid(row=0, column=0, columnspan=3, padx=10, pady=3, sticky=stickyFill)
                         tk.Label(inserterWindow, text="Page number where new page will be inserted:").grid(row=2, column=0, padx=10, pady=3)
                         pageToInsert = tk.Entry(inserterWindow)
                         pageToInsert.grid(row=2, column=1, sticky=stickyFill, pady=5, padx=5)
                         tk.Label(inserterWindow, text="Select PDF file with page to be inserted:").grid(row=3, column=0, padx=10, pady=3)
                         fileWithInsert = tk.Entry(inserterWindow)
                         fileWithInsert.grid(row=3, column=1, sticky=stickyFill, pady=5, padx=5)
                         tk.Button(inserterWindow, text="Browse...", command=lambda entry=fileWithInsert, window=inserterWindow: filePicker(entry, window)).grid(row=3, column=2, pady=
inserterWindow.mainloop()
                         filename = updateFile.get()
                         filename = filename[:-4] + '-updated.pdf'
                         updateFile = checkExist(updateFile.get())
                         pageToInsert = int(pageToInsert.get())
                         fileWithInsert = checkExist(fileWithInsert.get())
                         pageWithInsert = int(pageWithInsert.get())
                         if pageToInsert == 0 or pageWithInsert == 0:
                             popup("invalid page number, must be greater than 0")
17 of 24                                                                                                                                                            7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                https://www.programcreek.com/python/example/...
                         originalPDF = PyPDF2.PdfFileReader(updateFile)
                         PDFwithInsert = PyPDF2.PdfFileReader(fileWithInsert)
                         for i in range(updatedPDF.getNumPages()):
                                pdfOut.addPage(updatedPDF.getPage(i))
                         pdfOut.write(outputFile)
                         outputFile.close()
                         inserterWindow.destroy()
                         finished(filename, "Page insert", inserterWindow)
Example 70
                     def deletePage():
                         deleterWindow = tk.Tk()
                         deleterWindow.title("PDF page deleter")
tk.Label(deleterWindow, text="Deletes a single page inside an existing PDF").grid(row=0, column=0, columnspan=3, padx=10, pady=3, sticky=stickyFill)
deleterWindow.mainloop()
                         filename = updateFile.get()
                         filename = filename[:-4] + '-updated.pdf'
                         updateFile = checkExist(updateFile.get())
                         pageToDelete = int(pageToDelete.get())
                         if pageToDelete == 0:
                                popup("invalid page number, must be greater than 0")
originalPDF = PyPDF2.PdfFileReader(updateFile)
                         for i in range(updatedPDF.getNumPages()):
                             if i != pageToDelete - 1:
                                    pdfOut.addPage(updatedPDF.getPage(i))
                         pdfOut.write(outputFile)
                         outputFile.close()
                         deleterWindow.destroy()
                         finished(filename, "Page delete", deleterWindow)
Example 71
                         Returns
                         -------
                         The file path to the PDF
                         """
                         packet = io.BytesIO()
                         c = canvas.Canvas(packet)
                         for i in range(1, len(lngths)-1):
                             stringl = "{}".format(abs(round(lngths[i],1)))
                             stringp = "{}".format(abs(round(perimeters[i],1)))
                             c.drawString(360+((i-1)*27), 474-((i-1)*41.5), stringl)
                             c.drawString(88, 524.5- ((i-1)*74.5), stringp)
                         stringmaxZ = "{}".format(abs(round(lngths[len(lngths)-1],1)))
                         c.drawString(514, 419, stringmaxZ)
18 of 24                                                                                                                                                            7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                     https://www.programcreek.com/python/example/...
                         c.setFont("Courier-Bold", 12)
                         c.drawString(65, 575, "Perimeter / cm")
                         c.drawString(400, 520, "Distance / cm")
                         c.showPage()
                         c.drawImage("ant.png", 38,225, 256,256)
                         c.drawImage("lat.png", 300,225,256,256)
                         c.drawImage("figure.png", -2.5,-50, 334,200)
                         for i in range(1,len(CSA),2):
                             sCSA = "{}".format(round(CSA[i],1))
                             sAPW = "{}".format(round(APW[i],1))
                               sMLW = "{}".format(round(MLW[i],1))
                               c.drawString(403, 145-((i-1)*11.5), sCSA)
                               c.drawString(465, 145-((i-1)*11.5), sAPW)
                             c.drawString(520, 145-((i-1)*11.5), sMLW)
                         c.save()
                         packet.seek(0)
                         newpdf = PdfFileReader(packet)
                         template = PdfFileReader(open(os.path.join("res", "Measurements Template.pdf"), "rb"))
                         t2 = PdfFileReader(open(os.path.join("res", "Output Template.pdf"), "rb"))
                         output = PdfFileWriter()
                         page = t2.getPage(0)
                         page.mergePage(newpdf.getPage(1))
                         page2 = template.getPage(0)
                         page2.mergePage(newpdf.getPage(0))
                         output.addPage(page)
                         output.addPage(page2)
outputStream.close()
return output_file_path
Example 72
Project: pdf2pdfocr Author: LeoFCardoso File: pdf2pdfocr.py Apache License 2.0 4 votes
Example 73
Project: pdf2pdfocr Author: LeoFCardoso File: pdf2pdfocr.py Apache License 2.0 4 votes
def autorotate_final_output(self):
19 of 24                                                                                                                                                            7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                   https://www.programcreek.com/python/example/...
Example 74
Project: pdf2pdfocr Author: LeoFCardoso File: pdf2pdfocr.py Apache License 2.0 4 votes
                     def edit_producer(self):
                               self.debug("Editing producer")
                               param_source_file = self.tmp_dir + self.prefix + "-OUTPUT-ROTATED.pdf"
                               file_source = open(param_source_file, 'rb')
                               pre_output_pdf = PyPDF2.PdfFileReader(file_source, strict=False)
                               final_output_pdf = PyPDF2. PdfFileWriter()
                               for i in range(pre_output_pdf.getNumPages()):
                                   page = pre_output_pdf.getPage(i)
                                   final_output_pdf.addPage(page)
                               info_dict_output = dict()
                               # Our signature as a producer
                               our_name = "PDF2PDFOCR(github.com/LeoFCardoso/pdf2pdfocr)"
                               read_producer = False
                               producer_key = "/Producer"
                               if self.input_file_metadata is not None:
                                    for key in self.input_file_metadata:
                                        value = self.input_file_metadata[key]
                                         if key == producer_key:
                                               if type(value) == ByteStringObject:
                                                   value = str(value, errors="ignore")
                                                   value = "".join(filter(lambda x: x in string.printable, value))                  # Try to remove unprintable
                                               value = value + "; " + our_name
                                               read_producer = True
                                         #
                                         try:
                                             # Check if value can be accepted by pypdf API
                                             PyPDF2.generic.createStringObject(value)
                                             info_dict_output[key] = value
                                         except TypeError:
                                             # This can happen with some array properties.
                                             eprint("Warning: property " + key + " not copied to final PDF")
                               #
                               if not read_producer:
                                   info_dict_output[producer_key] = our_name
                               #
                               final_output_pdf.addMetadata(info_dict_output)
                               #
                               with open(self.output_file, 'wb') as f:
                                   final_output_pdf.write(f)
                                   f.close()
                               #
                               file_source.close()
Example 75
Project: Sudoku Author: BurnYourPc File: burnSudo2Img.py GNU General Public License v3.0 4 votes
def BurnSudoOnPdf(path,numpage,diffarray):
20 of 24                                                                                                                                                          7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                  https://www.programcreek.com/python/example/...
pdf = PdfFileWriter()
                         pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
                         pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf'))
                         pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf'))
                         #pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))
                         imgDoc.setFont('Vera', 13)
                         imgDoc.drawString(30,820,"BurnYourPc Organization/")
                         imgDoc.setFont('VeraBd', 9)
                         imgDoc.drawString(197,820,"Sudoku Project")
                         imgDoc.setFont('VeraIt', 8)
                         imgDoc.drawString(430,20,"By PantelisPanka, nikfot, TolisChal")
                         imgDoc.setFont('Vera', 8)
                         imgDoc.drawString(550,820,str(numpage))
imgDoc.save()
pdf.write(open("output"+ str(numpage)+".pdf","wb"))
Example 76
'''
try:
21 of 24                                                                                                                             7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                            https://www.programcreek.com/python/example/...
MakeFITS(self)
except:
self.exception_handler(self.debug)
Example 77
                     def pdf_cropper_title(sourth_path,destin_path):
                         Path_extracted=Address(1).split("\n")
                         Path_extracted1=Path_extracted[0]
                         source_pdf_Dir=os.path.join(Path_extracted1,sourth_path)
                         destin_pdf_Dir=os.path.join(Path_extracted1,destin_path)
                         basic_search_words = r'(?:product description|Product description|Product Description|PRODUCT DESCRIPTION|general description|General description|General Description|GENERAL
                         non_crop_words=r'(?:TABLE OF CONTENTS|Table Of Contents|Table of Contents|Table of contents|table of contents)'
                         for pdf in os.listdir(source_pdf_Dir):
                             #print("ALL PDF"+pdf)
                             single_source_name = os.path.join(source_pdf_Dir , pdf)
                               single_destin_name = os.path.join(destin_pdf_Dir , pdf)
                               pdf1 = PdfFileReader(open(single_source_name,'rb'),strict=False)
                               if pdf1.isEncrypted:
                                     try:
                                         pdf1.decrypt('')
                                     except:
                                         command = ("cp "+ single_source_name +
                                          " temp.pdf; qpdf --password='' --decrypt temp.pdf " + single_source_name
                                          + "; rm temp.pdf")
                                          os.system(command)
                                          pdf1 = PdfFileReader(open(single_source_name,'rb'),strict=False)
                               page_number=pdf1.getNumPages()
                               pdf_writer = PdfFileWriter()
                               find_page_tag=False
                               for i in range(0, page_number):
                                     PageObj = pdf1.getPage(i)
                                     Text = PageObj.extractText()
                                     if re.search(basic_search_words, Text):
                                         # print("document_name"+pdf)
                                          #print("salam"+str(i))
                                          find_page_tag=True
                                          target_page=i
                                       break
                               if find_page_tag:
                                   pdf_writer.addPage(pdf1.getPage(target_page))
                                   with open(single_destin_name, 'wb') as out:
                                       pdf_writer.write(out)
                               else:
                                     if page_number>=2:
                                          for page in range(0, 2):
                                              PageObj = pdf1.getPage(page)
                                              Text = PageObj.extractText()
                                                if not re.search(non_crop_words, Text):
                                                     pdf_writer.addPage(pdf1.getPage(page))
                                                else:
                                                    pass
                                         with open(single_destin_name, 'wb') as out:
                                             pdf_writer.write(out)
                                     else:
                                          copyfile(single_source_name, single_destin_name)
Example 78
Project: deda Author: dfd-tud File: privacy.py GNU General Public License v3.0 4 votes
                     def _createMask(self,page=None):
                             w,h = self.pagesize
                             w = float(w)
                               h = float(h)
                               shps = self.hps*self.scale
                               svps = self.vps*self.scale
                               io = BytesIO()
                               c = canvas.Canvas(io, pagesize=(w,h) )
                               c.setStrokeColorRGB(*self.colour)
                               c.setFillColorRGB(*self.colour)
                               allDots = []
                               for x_ in range(int(w/shps/72+1)):
                                   for y_ in range(int(h/svps/72+1)):
                                       for xDot, yDot in self.proto:
                                           x = (x_*shps+xDot*self.scale)
                                           y = (y_*svps+yDot*self.scale)
                                            if x*72 > w or y*72 > h: continue
                                           allDots.append((x,y))
22 of 24                                                                                                                                                            7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                                    https://www.programcreek.com/python/example/...
Example 79
                     def sign_pdf(args):
                         #TODO: use a gui or something.... for now, just trial-and-error the coords
                            page_num, x1, y1, width, height = [int(a) for a in args.coords.split("x")]
                            page_num -= 1
                            pdf = PyPDF2.PdfFileReader(pdf_fh)
                            writer = PyPDF2. PdfFileWriter()
                            sig_tmp_filename = None
                                if i == page_num:
                                     # Create PDF for signature
                                     sig_tmp_filename = _get_tmp_filename()
                                     c = canvas.Canvas(sig_tmp_filename, pagesize=page.cropBox)
                                     c.drawImage(args.signature, x1, y1, width, height, mask='auto')
                                     if args.date:
                                         c.drawString(x1 + width, y1, datetime.datetime.now().strftime("%Y-%m-%d"))
                                     c.showPage()
                                     c.save()
writer.addPage(page)
Example 80
                     Project: cryptoluggage Author: miguelinux314 File: pypdfocr_pdf.py   GNU General Public License
                                                                                                                        4 votes
                     v3.0
                               text_pdf_filenames = []
                               for img_filename, hocr_filename in hocr_filenames:
                                   text_pdf_filename = self.overlay_hocr_page(dpi, hocr_filename, img_filename)
                                   logging.info("Created temp OCR'ed pdf containing only the text as %s" % (text_pdf_filename))
                                   text_pdf_filenames.append(text_pdf_filename)
                               writer = PdfFileWriter()
                               orig = open(orig_pdf_filename, 'rb')
                               text_file = open(all_text_filename, 'rb')
23 of 24                                                                                                                                               7/5/20, 9:10 PM
PyPDF2.PdfFileWriter Python Example                                                                  https://www.programcreek.com/python/example/...
writer.write(f)
                         orig.close()
                         text_file.close()
                         # Windows sometimes locks the temp text file for no reason, so we need to retry a few times to delete
                         for fn in text_pdf_filenames:
                             #os.remove(fn)
                             Retry(partial(os.remove, fn), tries=10, pause=3).call_with_retry()
                         os.remove(all_text_filename)
                         logging.info("Created OCR'ed pdf as %s" % (pdf_filename))
return pdf_filename
24 of 24 7/5/20, 9:10 PM