Search This Blog

Saturday, October 4, 2025

Create a Form Using Python for Save Data into Excel like a Database



#Download Pyhton from here https://www.python.org/downloads/ 

#Download Python:

#Click the “Download Python 3.x.x” button (the latest version).

#Run the Installer:

#Double-click the downloaded .exe file.

#Important: ✅ Check the box that says “Add Python to PATH”.

#Click “Install Now”.

# CMD : python --version

#After installing Python install tkinter  library using below command into CMD 

#    pip install openpyxl

#Write below code into a text book with extension .py

#That's it now dubble click and enjoy


import tkinter as tk

from tkinter import messagebox

import os

from openpyxl import Workbook, load_workbook


# Constants

EXCEL_FILE = "data.xlsx"

HEADERS = ["Name", "Email", "Phone"]


# Function to create Excel file if it doesn't exist

def setup_excel():

    if not os.path.exists(EXCEL_FILE):

        wb = Workbook()

        ws = wb.active

        ws.append(HEADERS)

        wb.save(EXCEL_FILE)


# Function to save data to Excel

def save_data():

    name = name_var.get().strip()

    email = email_var.get().strip()

    phone = phone_var.get().strip()


    if not (name and email and phone):

        messagebox.showerror("Input Error", "All fields are required!")

        return


    wb = load_workbook(EXCEL_FILE)

    ws = wb.active

    ws.append([name, email, phone])

    wb.save(EXCEL_FILE)


    # Clear fields

    name_var.set("")

    email_var.set("")

    phone_var.set("")

    messagebox.showinfo("Success", "Data saved to Excel successfully!")


# GUI setup

setup_excel()

root = tk.Tk()

root.title("Data Entry Form")

root.geometry("300x250")


# Tkinter variables

name_var = tk.StringVar()

email_var = tk.StringVar()

phone_var = tk.StringVar()


# UI Elements

tk.Label(root, text="Enter your details", font=("Helvetica", 14)).pack(pady=10)


tk.Label(root, text="Name").pack()

tk.Entry(root, textvariable=name_var).pack()


tk.Label(root, text="Email").pack()

tk.Entry(root, textvariable=email_var).pack()


tk.Label(root, text="Phone").pack()

tk.Entry(root, textvariable=phone_var).pack()


tk.Button(root, text="Submit", command=save_data).pack(pady=10)


root.mainloop()


No comments:

Post a Comment

Create a Form Using Python for Save Data into Excel like a Database

#Download Pyhton from here https://www.python.org/downloads/  #Download Python: #Click the “Download Python 3.x.x” button (the latest versio...