Home Developer Programming Language Framework Articles Contact Me

HTML CSS JAVASCRIPT JAVA PYTHON JQUERY REACTJS VUEJS ANGULARJS EMBERJS SASS LESS STYLUS GITHUB GITLAB BIGBUCKET JAVA KOTLIN HADOOP JDBC SERVLETS JAVA BEANS(EJB) JAVA SERVER PAGES(JSP) UNIX LINUX POSTGRESQL MSSQLSERVER SQLITE MONGODB XML FLASK PYTHON MYSQL JAVA C++ JUNIT SELENIUM

Convert PDF to CSV or Excel Using Tkinter in Python

Prerequisite:-

In this article, we will learn the PDF File conversion; PDF to Excel or PDF to CSV Using Tkinter in python

Libraries Required:-

  1. Tkinter
  2. Tabula

Installation:-

pip install tabula

Approach:-

  1. For PDF file Conversion we will use tabula Module
  2. PDF to Excel use to_excel() method.
  3. PDF to CSV use to_csv() method.
Below is how the GUI looks like:-

to_excel():- It is used to convert PDF file into Excel File

Syntax:-

# Enter Excel File Name with extension
to_excel("File Name",**attr)

to_csv():- It is used to convert PDF file into CSV File

Syntax:-

# Enter Excel File Name with extension
to_csv("File Name",**attr)

Let's Understand Step by Step Implementation:-

Step 1(Create Tkinter window; Add Buttons, Labels, etc...)

# Import Module
from tkinter import *

# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')

# Add Labels and Buttons
Label(root, text="PDF CONVERSION", font="italic 15 bold").pack(pady=10)

Button(root,text="Select PDF File",font=14).pack(pady=10)

frame = Frame()
frame.pack(pady=20)

excel_btn = Button(frame,text="PDF to Excel",relief="solid",bg="white",font=15)
excel_btn.pack(side=LEFT,padx=10)

csv_btn = Button(frame,text="PDF to CSV",relief="solid",bg="white",font=15)
csv_btn.pack()

# Execute Tkinter
root.mainloop()

Step 2 (Define Functions)

Now We will create three functions; select file(), pdf_to_excel(), pdf_to_csv()

  1. select_file:- It is used for selecting the PDF File
  2. pdf_to_excel:- It is used to convert Pdf into an excel file
  3. pdf_to_csv:- It is used to convert Pdf into CSV file
def select_file():
    global file_name
    file_name = askopenfilename(initialdir = "/",title = "Select File")

# PDF TO EXCEL
def pdf_to_excel():
    if file_name.endswith('.pdf'):
        # Read PDF File
        # this contain a list
        df = tabula.read_pdf(file_name, pages = 1)[0]

        # Convert into Excel File
        df.to_excel('Excel.xlsx')

# PDF TO CSV
def pdf_to_csv():
    if file_name.endswith('.pdf'):
        # Read PDF File
        # this contain a list
        df = tabula.read_pdf(file_name, pages = 1)[0]

        # Convert into CSV File
        df.to_csv('CSV.csv')

Below is the Implementation:-

# Import Module
from tkinter import *
from tkinter.filedialog import askopenfilename
import tabula

# Create Object
root = Tk() 
# set Geometry
root.geometry('400x200')

def select_file():
    global file_name
    file_name = askopenfilename(initialdir = "/",title = "Select File")

# PDF TO EXCEL
def pdf_to_excel():
    if file_name.endswith('.pdf'):
        # Read PDF File
        # this contain a list
        df = tabula.read_pdf(file_name, pages = 1)[0]

        # Convert into Excel File
        df.to_excel('Excel.xlsx')

# PDF TO CSV
def pdf_to_csv():
    if file_name.endswith('.pdf'):
        # Read PDF File
        # this contain a list
        df = tabula.read_pdf(file_name, pages = 1)[0]

        # Convert into CSV File
        df.to_csv('CSV.csv')


# Add Labels and Buttons
Label(root, text="PDF CONVERSION", font="italic 15 bold").pack(pady=10)

Button(root,text="Select PDF File",command=select_file,font=14).pack(pady=10)

frame = Frame()
frame.pack(pady=20)

excel_btn = Button(frame,text="PDF to Excel",command=pdf_to_excel,relief="solid",
                   bg="white",font=15)
excel_btn.pack(side=LEFT,padx=10)

csv_btn = Button(frame,text="PDF to CSV",command=pdf_to_csv,relief="solid",
                 bg="white",font=15)
csv_btn.pack()

# Execute Tkinter
root.mainloop()

Input:-

PDF file:- click here



Output:-

EXCEL File



CSV File