Header Ads Widget

PDF Related codes in PY

 1. Text & Pdf Converter


import pyPDF2
from fpdf import FPDF
from pyPDF2 import PdfFileWriter, PdfFileReader

class Text_Pdf(object):
     def __int__(self, file_path):
          self.file_path = file_path
          self.file-name = self.file_path.split("\\")[-1]
          self.file_ext = self.file_name.split(".")[-1]
          self.main()

    def __repr__(self):
          return f"{self.file_name} Converted..."

    def main(self):
          if self.file_ext == "pdf":
             pdf = PyPDF2.PdfFileReader(open(self.file_path, "rb"))
             with open(f"{self.file_name.strip('.pdf')}.txt", "a") as file:
                    for page in pdf.pages:
                         file.write(page.extractText()) 

          else:
                    pdf_converter = FPDF()
                    pdf_converter.add_page()
                    pdf_converter.set_font("Arial", size=15)
                    with open(self.file_path, "r") as file:
                          for row, line in enumerate(file):
                               line = line.encode('latin-1', 'replace').decode('latin-1')
                               pdf_converter.cell(200, 10, txt=line, ln=row, align="c")
                   return pdf_converter.output(f"{self.file_name.strip('.txt')}.pdf")

if __name__ == "__main__":
     file = input("Drag and Drop file here:\n>> ")
     pdf_tool = Text_Pdf(file)
     print(pdf_tool)

Output :-


This will transfer text to pdf in your saved files.


2. Password Protect PDF


from fpdf import FPDF
from PyPDF2 import PdfFileWriter, PdfFileReader

def securepdf(file, password):
     parser = PdfFileWriter()
     pdf = PdfFileWriter(file)

     for page in range(pdf.numPages):
          parser.addPage(pdf.getPage(page))
     parser.encrypt(password)
     with open(f"encrypted_{file}", "wb") as f:
            parser.write(f)
     print(f"encrypted_{file} Created...")

if __name__ == "__main__":
     file = "kivy.pdf"
     password = "Anonymous__programmer"
     secure_pdf(file, password)


Output :-


This will set password for your file.


3. Pic & PDF Convert


import os
import pdf2image
from PIL import Image
from pdf2image import convert_from_path

class Pic_Pdf(object):
     def __init__(self, file_path):
          self.file_path = file_path.split("\\")
          self.file_name = self.file_path[-1]
          self.file_ext = self.file_name.split(".")[-1]
          self.file_path = os.path.join(*self.file_path)
          self.main()

    def __repr__(self):
         return f"{self.file_name} Converted..."

    def main(self):
          if self.file_ext == "pdf":
             pages = convert_from_path(self.file_path, 500)
             for page in pages:
                  page.save(self.file_path.replace(".pdf", ".png"), "PNG")

          else:
             picture = Image.open(self.file_path)
             parsed = picture.convert("RGB")
             return parsed.save(self.file_path.replace(f".{self.file_ext}", ".pdf"))

if __name__ == "__main__":
   print("\nDrag and Drop file here:")
   file = input(r" >> ")
   pdf_tool = Pic_Pdf(file)
   print(pdf_tool)


Output :-


This will Convert Pic into pdf in your compiler files.


4. DOCX & PDF Convert


import os
import PyPDF2
from pdf2docx import parse

class Docx_Pdf(object):
     def __init__(self, file_path):
          self.file_path = file_path.split("\\")
          self.file_name = self.file_path[-1]
          self.file_ext() = self.file_name.split(".")[-1]
          self.file_path = os.path.join(*self.file_path)
          self.main()

    def __repr__(self):
          return f"{self.file_name} Converted..."

    def main(self):
          if self.file_ext == "pdf":
             pdf = PyPDF2.PdfFileReader(open(self.file_path, "rb"))
             with open (self.file_path.replace(".pdf", ".docx"), "a") as file:
                    for page in pdf.pages:
                         file.write(page.extractText())
          else:
              new_filename = self.file_path.replace(".pdf', ".docx")
              return parse(self.file_path, new_filename, start=0, end=1)

if __name__ == "__main__":
      file = input(r"Drag and Drop file here:\n >> ")
      pdf_tool = Docx_Pdf(file)
      print(pdf_tool)  


Output :-


This will Document file to PDF file while you uploading it on proper location and saved in your files.
    

Post a Comment

0 Comments