GUI to Send Data to an Excel File with Python
Building a graphical interface with Tkinter to capture data from a form and write it to an Excel spreadsheet using openpyxl — a typical use case: collecting records in a plant, laboratory, or survey without relying on proprietary tools.
The ConnectExcel class builds a minimalist window with 3 fields (Email, Address, Gender), 3 buttons (Open file, Send, Save changes), and persists everything to a .xlsx file.
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter.ttk import Combobox
from openpyxl import load_workbook
class ConnectExcel:
def __init__(self):
self.root = tk.Tk()
self.root.geometry('480x210+440+240')
self.root.configure(bg = '#8CD4D7')
self.root.title('Connect Excel')
# Labels
self.label1 = tk.Label(self.root, text = "Email", bg = '#8CD4D7')
self.label1.place(x = 140, y = 40)
self.label2 = tk.Label(self.root, text = "Address", bg = '#8CD4D7')
self.label2.place(x = 140, y = 60)
self.combobox_label3 = tk.Label(self.root, text = "Gender", bg = '#8CD4D7')
self.combobox_label3.place(x = 140, y = 80)
# Entries
self.entry1 = tk.Entry(self.root, width = 23)
self.entry1.place(x = 225, y = 40)
self.entry2 = tk.Entry(self.root, width = 23)
self.entry2.place(x = 225, y = 60)
# Combobox
self.combobox = Combobox(self.root, values = ['M', 'F'], state = 'r', width = 20)
self.combobox.place(x = 225, y = 80)
self.combobox.set('M')
# Buttons
self.open_file_button = tk.Button(self.root, text = "Open file", command = self.open_excel)
self.open_file_button.place(x = 4, y = 4)
self.submit_button = tk.Button(self.root, text = "Send", command = self.send_data)
self.submit_button.place(x = 190, y = 160)
self.save_button = tk.Button(self.root, text = "Save changes", command = self.save_changes)
self.save_button.place(x = 260, y = 160)
self.root.mainloop()
def open_excel(self):
try:
self.file_path = filedialog.askopenfilename()
self.workbook = load_workbook(filename = self.file_path)
self.sheet = self.workbook.active
except Exception as e:
print(f'The error is: {e}')
def save_changes(self):
try:
self.workbook.save(filename = self.file_path)
except:
messagebox.showinfo("Notification", "Add an Excel file to save the data.")
def send_data(self):
try:
data1 = self.entry1.get()
data2 = self.entry2.get()
data3 = self.combobox.get()
row = [data1, data2, data3]
self.sheet.append(row)
self.entry1.delete(0, tk.END)
self.entry2.delete(0, tk.END)
except:
messagebox.showinfo("Notification", "Add an Excel file to start data storage.")
if __name__ == '__main__':
ConnectExcel()
The resulting window (mockup):

Usage flow:
- The user clicks Open file →
filedialog.askopenfilename()opens a native system dialog. The.xlsxis loaded withopenpyxl.load_workbookand the active sheet is taken. - User enters data and clicks Send →
send_data()reads the three fields, builds a row, and appends it withsheet.append(row). It also clears the Entry fields for the next record. - When finished loading records, user clicks Save changes →
workbook.save(file_path)persists everything to disk.
Design points to highlight:
- Separating open and save allows accumulating multiple rows in memory before writing to disk — avoids opening/closing the file on each record and reduces corruption risk.
- The
try/exceptblocks aroundopen_excelandsend_dataprotect the user from the most common error: clicking "Send" without opening a file first. Comboboxwithstate='r'(readonly) prevents the user from typing a value outside the valid domain (M,F).
Natural improvements:
- Validate the email with a regex before saving
- Automatically add a header row if the file is empty
- Use
ttk.Entryinstead oftk.Entryto respect the system theme - Display the opened file path in the status bar
Installation:
pip install openpyxl
tkinter comes bundled with Python on Windows and Mac. On Linux, use sudo apt install python3-tk.