0% found this document useful (0 votes)
180 views1 page

PDF To Text Logic

This document contains Python code to extract text from a PDF file, open a text file, and convert the text to a new PDF file. The code uses PyPDF2 to extract text from a selected PDF file. It then defines a function to take the text file as a parameter, add the text to a new PDF using FPDF with different fonts for short and long text, and save the output as a new PDF file. The code opens a sample text file and calls the conversion function to generate the new PDF.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views1 page

PDF To Text Logic

This document contains Python code to extract text from a PDF file, open a text file, and convert the text to a new PDF file. The code uses PyPDF2 to extract text from a selected PDF file. It then defines a function to take the text file as a parameter, add the text to a new PDF using FPDF with different fonts for short and long text, and save the output as a new PDF file. The code opens a sample text file and calls the conversion function to generate the new PDF.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import PyPDF2

import os
from tkinter import *
from tkinter import filedialog
from pypdf import PdfReader

hello=filedialog.askopenfilename(initialdir=os.getcwd(),title='Select Image
File',filetype=(("PNG file","*.pdf"),("JPG file","*.pdf"),("All Files","*.pdf")))
a = PyPDF2.PdfReader(hello)
extracted_text=a.pages[1].extract_text()

from fpdf import FPDF


from tkinter import *
from tkinter import filedialog
import os

def convert_file(file):
pdf = FPDF
pdf.add_page()

for text in file:


if len(text) <= 20:
pdf.set_font("Arial", "В", size=18) # For title te
pdf.cell(w=288, h=10, txt=text, Ln=1, align="C")
else:
pdf.set_font("Aria1",size=15) # For paragraph
pdf.multi_cell(w=0, h=10, txt=text, align="L")
pdf.output("output. pdf")
print("successfully converted!")
#File=filedialog.askopenfilename(initialdir=os.getcwd(),title='Select Image
File',filetype=(("PNG file","*.txt"),("JPG file","*.txt"),("All Files","*.txt")))
File = open("sample.txt","r")
convert_file(File)

You might also like