コード例 #1
0
def combina_foi_din_diferite_pdf():
    """1. Find all pdf in a folder."""
    pdfFiles = []
    pdfWriter = PyPDF3.PdfFileWriter()
    for filename in os.listdir('.'):
        if filename.endswith('.pdf'):
            pdfFiles.append(filename)
    pdfFiles.sort(key=str.lower)
    #print(pdfFiles)
    pdfFiles = ['3340063.pdf', 'bus.pdf']  # pentru test
    print(pdfFiles)  # pentru test
    # pdfWriter = PyPDF3.PdfFileWriter()
    # """2. Open each pdf."""
    # pdfFiles = []
    for filename in pdfFiles:
        pdfFileObj = open(filename, 'rb')
        pdfReader = PyPDF3.PdfFileReader(pdfFileObj)
        print(pdfReader)
    # """3. Add each page."""
    for filename in pdfFiles:
        for pageNum in range(1, pdfReader.numPages):
            pageObj = pdfReader.getPage(pageNum)
            pdfWriter.addPage(pageObj)
        print(pdfWriter)
    # """4. Save the result."""
    for filename in pdfFiles:
        for pageNum in range(1, pdfReader.numPages):
            pdfOutput = open('rezultat_merging.pdf', 'wb')
            pdfWriter.write(pdfOutput)
            pdfOutput.close()
コード例 #2
0
    def add_barcode(self, barcode, original_path, x_pos=None, y_pos=None):
        """
        Adds a barcode to the first page of a document

        :param barcode: Barcode to add to the file specified in original_path
        :param original_path: Path to the original file.
        :param y_pos: Position of the bottom of the barcode measure in mm from the bottom of the page.
        :return:
        """
        merger = PyPDF3.PdfFileReader(original_path)
        barcode_doc = PyPDF3.PdfFileReader(
            self.barcode_doc.generate_barcode(barcode)).getPage(0)
        first_page = merger.getPage(0)
        if y_pos is not None:
            barcode_target_y = self.get_y_pos(first_page.mediaBox[3], y_pos)
        else:
            barcode_target_y = 0
        centre_pos = float(first_page.mediaBox[2]) / 2 - float(
            barcode_doc.mediaBox[2]) / 2
        first_page.mergeTranslatedPage(barcode_doc, centre_pos,
                                       barcode_target_y)
        out = PyPDF3.PdfFileWriter()
        out.addPage(first_page)
        [out.addPage(page) for page in merger.pages[1:]]
        filename = original_path[original_path.rfind('/'):original_path.
                                 rfind('.')]
        output_filename = f"{self.save_path}/{filename}_{barcode}_coded.pdf"
        with open(output_filename, 'wb') as output:
            out.write(output)
コード例 #3
0
def decrypt():
    pdfName = input('Enter the name of PDF file to decrypt:\n')
    pdfName = path.abspath(pdfName)

    # Check if entered filename is valid
    if not path.exists(pdfName) or pdfName[-4:].lower() != '.pdf':
        print(f'The filename {pdfName} is not a PDF.')
        sys.exit()

    pdfFile = open(pdfName, 'rb')
    pdfReader = PyPDF3.PdfFileReader(pdfFile)

    # Password for decryption
    password = input('Enter the password to decrypt the PDF: \n')
    pdfReader.decrypt(password)
    pdfWriter = PyPDF3.PdfFileWriter()

    # Loop through the pages and add them to pdfWriter
    for pageNum in range(pdfReader.numPages):
        pdfWriter.addPage(pdfReader.getPage(pageNum))

    # Save the resulting PDF to a file
    decryptedPdf = open('decrypted' + path.basename(pdfName), 'wb')
    pdfWriter.write(decryptedPdf)
    decryptedPdf.close()
    print(f'File decrypted and saved as decrypted{path.basename(pdfName)}')
コード例 #4
0
def encripteaza(pdf_file):
    pdfFile = open(pdf_file, 'rb')
    pdfReader = PyPDF3.PdfFileReader(pdfFile)
    pdfWriter = PyPDF3.PdfFileWriter()
    for pageNum in range(pdfReader.numPages):
        pdfWriter.addPage(pdfReader.getPage(pageNum))
    pdfWriter.encrypt('swordfish')  # parola cu care encripteaza
    resultPdf = open('rezultat_encriptare.pdf', 'wb')
    pdfWriter.write(resultPdf)
    resultPdf.close()
コード例 #5
0
def roteste_pagina(pdf_file):  # roteste prima pagina si face din ea un nou pdf
    minutesFile = open(pdf_file, 'rb')
    pdfReader = PyPDF3.PdfFileReader(minutesFile)
    page = pdfReader.getPage(0)
    page.rotateClockwise(90)
    pdfWriter = PyPDF3.PdfFileWriter()
    pdfWriter.addPage(page)
    resultPdfFile = open('rezultat_rotire.pdf', 'wb')
    pdfWriter.write(resultPdfFile)
    resultPdfFile.close()
    minutesFile.close()
コード例 #6
0
def overlay(pdf_file):
    minutesFile = open(pdf_file, 'rb')
    pdfReader = PyPDF3.PdfFileReader(minutesFile)
    minutesFirstPage = pdfReader.getPage(0)
    pdfWatermarkReader = PyPDF2.PdfFileReader(open('watermark.pdf', 'rb'))
    minutesFirstPage.mergePage(pdfWatermarkReader.getPage(0))
    pdfWriter = PyPDF3.PdfFileWriter()
    pdfWriter.addPage(minutesFirstPage)
    for pageNum in range(1, pdfReader.numPages):
        pageObj = pdfReader.getPage(pageNum)
        pdfWriter.addPage(pageObj)
    resultPdfFile = open('rezultat_overlay.pdf', 'wb')
    pdfWriter.write(resultPdfFile)
    minutesFile.close()
    resultPdfFile.close()
コード例 #7
0
ファイル: TestClass.py プロジェクト: GuyDahan1/testproject
 def __crop__(self, name):
     pagNum = int(input("Enter page number\n"))
     upperX = int(input("please enter upper x cordinate\n"))
     upperY = int(input("please enter upper y cordinate\n"))
     lowerX = int(input("please enter lower x cordinate\n"))
     lowerY = int(input("please enter lower y cordinate\n"))
     path = self.getPath()
     writer = PyPDF3.PdfFileWriter()
     page = self.tPdf.getPage(pagNum)
     page.cropBox.setLowerLeft((lowerX, lowerY))
     page.cropBox.setUpperRight((upperX, upperY))
     writer.addPage(page)
     path = "Class\pdfFileHere\{0}.pdf".format(name)
     outstream = open(path, 'wb')
     writer.write((outstream))
     outstream.close()
コード例 #8
0
def append_pdf():  # append a pdf to another pdf (merge 2 pdf-uri)
    pdf1File = open('meetingminutes.pdf', 'rb')
    pdf2File = open('meetingminutes2.pdf', 'rb')
    pdf1Reader = PyPDF3.PdfFileReader(pdf1File)
    pdf2Reader = PyPDF3.PdfFileReader(pdf2File)
    pdfWriter = PyPDF3.PdfFileWriter()
    for pageNum in range(pdf1Reader.numPages):
        pageObj = pdf1Reader.getPage(pageNum)
        pdfWriter.addPage(pageObj)
    for pageNum in range(pdf2Reader.numPages):
        pageObj = pdf2Reader.getPage(pageNum)
        pdfWriter.addPage(pageObj)
    pdfOutputFile = open('combinedminutes1.pdf', 'wb')
    pdfWriter.write(pdfOutputFile)
    pdfOutputFile.close()
    pdf1File.close()
    pdf2File.close()
コード例 #9
0
ファイル: glopan.py プロジェクト: mortenengen/glowing-pancake
def split_pdf(pdffile: str):
    """Split a PDF file in one file per page.

    Args:
        pdffile (str): The name of the PDF file to split.
    """
    with open(pdffile, 'rb') as pdffile_handle:
        pdf_in = pypdf.PdfFileReader(pdffile_handle)
        num_pages = pdf_in.numPages
        pages = []
        if 'pdf' in pdffile.lower():
            file_first_name = pdffile.lower()[:pdffile.lower().index('.pdf')]

        for page in range(num_pages):
            pages.append(file_first_name + f'_p_{page}' + '.pdf')
            pdf_out = pypdf.PdfFileWriter()
            pdf_out.addPage(pdf_in.getPage(page))

            with open(pages[-1], 'wb') as stream:
                pdf_out.write(stream)

    return pages
コード例 #10
0
def simple2():
    pdf_file = open('meetingminutes.pdf', 'rb')
    pdf_reader = PyPDF3.PdfFileReader(pdf_file)

    pdf_file2 = open('meetingminutes2.pdf', 'rb')
    pdf_reader2 = PyPDF3.PdfFileReader(pdf_file)

    pdf_writer = PyPDF3.PdfFileWriter()

    for page_num in range(pdf_reader.numPages):
        pdf_obj = pdf_reader.getPage(page_num)
        pdf_writer.addPage(pdf_obj)

    for page_num in range(pdf_reader2.numPages):
        pdf_obj = pdf_reader2.getPage(page_num)
        pdf_writer.addPage(pdf_obj)

    pdf_file3 = open('combinedminutes.pdf', 'wb')
    pdf_writer.write(pdf_file3)

    pdf_file3.close()
    pdf_file.close()
    pdf_file2.close()
コード例 #11
0
import PyPDF3, os

os.chdir('C:\\users\\rahul.goyal\\documents')

pdf1File = open('RBI-GradeB-Prep.pdf', 'rb')
pdf2File = open('Leave Policy ION India.pdf', 'rb')

reader1 = PyPDF3.PdfFileReader(pdf1File)
reader2 = PyPDF3.PdfFileReader(pdf2File)
writer = PyPDF3.PdfFileWriter()

for pageNum in range(reader1.numPages):
    page = reader1.getPage(pageNum)
    writer.addPage(page)

for pageNum in range(reader2.numPages):
    page = reader2.getPage(pageNum)
    writer.addPage(page)

outFile = open('Comb_Doc.pdf', 'wb')
writer.write(outFile)
outFile.close()
pdf2File.close()
pdf1File.close()
コード例 #12
0
      print("Found Email List File Named: "+file)
      emails = [x.replace('\n', '') for x in configStream]
  if not emails:
    raiseEmailFileError()
    os._exit(0)    
except:
  raiseEmailFileError()
  os._exit(0)
  
ln = pdf.getNumPages()
c = ln/len(emails)
if c % 1 != 0:
    raiseEmailCountError(ln, len(emails))
    os._exit(0)

c = int(c)
print("Detected "+str(c)+" Report Page(s) Per Student.")

for pg in range(0, ln, c):
  output = PyPDF3.PdfFileWriter()
  for i in range(0, c):
    output.addPage(pdf.getPage(pg+i))
  outputStream = BytesIO()
  output.write(outputStream)
  data = outputStream.getvalue()
  outputStream.close()
  mailPDF(emails[int(pg/c)], data, "report.pdf")
  pdf = PyPDF3.PdfFileReader(inputStream)

inputStream.close()