Created
May 29, 2023 09:20
-
-
Save Sven-Bo/71251c76e3e3e7183dc6135b371f8c14 to your computer and use it in GitHub Desktop.
This code is a modification of the initial code from the video tutorial titled "How to Create an Excel Data Entry Form in 10 Minutes Using Python (No VBA) | Easy & Simple" by Sven Bosau. It adds a timestamp next to the entry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This code is a modification of the initial code from the video tutorial: | |
"How to Create an Excel Data Entry Form in 10 Minutes Using Python (No VBA) | Easy & Simple" | |
Video link: https://youtu.be/svcv8uub0D0 | |
Modifications: | |
- Added a timestamp next to the entry in the Excel file | |
""" | |
from pathlib import Path | |
import PySimpleGUI as sg | |
import pandas as pd | |
from datetime import datetime | |
# Add some color to the window | |
sg.theme('DarkTeal9') | |
current_dir = Path(__file__).parent if '__file__' in locals() else Path.cwd() | |
EXCEL_FILE = current_dir / 'Data_Entry.xlsx' | |
# Load the data if the file exists, if not, create a new DataFrame | |
if EXCEL_FILE.exists(): | |
df = pd.read_excel(EXCEL_FILE) | |
else: | |
df = pd.DataFrame() | |
layout = [ | |
[sg.Text('Please fill out the following fields:')], | |
[sg.Text('Name', size=(15,1)), sg.InputText(key='Name')], | |
[sg.Text('City', size=(15,1)), sg.InputText(key='City')], | |
[sg.Text('Favorite Colour', size=(15,1)), sg.Combo(['Green', 'Blue', 'Red'], key='Favorite Colour')], | |
[sg.Text('I speak', size=(15,1)), | |
sg.Checkbox('German', key='German'), | |
sg.Checkbox('Spanish', key='Spanish'), | |
sg.Checkbox('English', key='English')], | |
[sg.Text('No. of Children', size=(15,1)), sg.Spin([i for i in range(0,16)], | |
initial_value=0, key='Children')], | |
[sg.Submit(), sg.Button('Clear'), sg.Exit()] | |
] | |
window = sg.Window('Simple data entry form', layout) | |
def clear_input(): | |
for key in values: | |
window[key]('') | |
return None | |
while True: | |
event, values = window.read() | |
if event == sg.WIN_CLOSED or event == 'Exit': | |
break | |
if event == 'Clear': | |
clear_input() | |
if event == 'Submit': | |
new_record = pd.DataFrame(values, index=[0]) | |
new_record['Timestamp'] = datetime.now() # add timestamp | |
df = pd.concat([df, new_record], ignore_index=True) | |
df.to_excel(EXCEL_FILE, index=False) # This will create the file if it doesn't exist | |
sg.popup('Data saved!') | |
clear_input() | |
window.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment