Back How to Create an Excel Data Entry Form in 10 Minutes Using Python (No VBA) | Easy & Simple

10-20
Created By: Coding Is Fun
Published At: 2021-05-22
Search Query: Python MongoDB Create Collection,Python MongoDB Create Database,Python MongoDB Drop Collection,Python MongoDB Drop Collection,Python Python MongoDB Delete
Tags: How to Create an Excel Data Entry Form in 10 Minutes Using Python (No VBA) | Easy & Simple coding is fun pysimple gui pysimplegui tutorial data entry form excel data entry form data entry from without vba data entry excel data entry excel tutorial data entry form in excel how to create excel forms python data entry form data entry in excel microsoft excel ecxel forms how to create data entry form in excel python gui excel gui python tutorial
Did you know you can use Python code to create an Excel Data Entry Form? This is a tutorial that will show you how to create one using the PySimpleGUI & Pandas library. No VBA or macros are required. At the end of the tutorial, we will also be converting the Python script to a standalone program. In doing so, we could share this data entry form with colleagues & friends, and they could use our program without having to install Python. The best part is that you could use this code as a starter template whenever you want to build a data entry form for Excel. If you want to add or remove columns, you only need to change one line of code. Additionally, you could also easily add more elements to the user form, like Multiline Input Fields, sliders, list boxes, a file dialogue box and much more.

PySimpleGUI Documentation:
👉 https://pysimplegui.readthedocs.io/en/latest/

Full code and sample files are available here:
👉 https://github.com/Sven-Bo/data-entry-form-pysimplegui

◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
𝗖𝗢𝗡𝗡𝗘𝗖𝗧 𝗪𝗜𝗧𝗛 𝗠𝗘:
🌎 Website: https://pythonandvba.com
📝 GitHub: https://github.com/Sven-Bo
⭐ Discord: https://pythonandvba.com/discord
▶️ Subscribe: https://youtube.com/c/CodingIsFun?sub_confirmation=1

🎉 𝗙𝗥𝗘𝗘 𝗘𝘅𝗰𝗲𝗹 𝗔𝗱𝗱-𝗶𝗻 𝘁𝗼 𝗯𝗼𝗼𝘀𝘁 𝘆𝗼𝘂𝗿 𝗽𝗿𝗼𝗱𝘂𝗰𝘁𝗶𝘃𝗶𝘁𝘆
Get it here: https://pythonandvba.com/mytoolbelt

📚 𝗥𝗲𝘀𝗼𝘂𝗿𝗰𝗲𝘀 𝗳𝗼𝗿 𝗹𝗲𝗮𝗿𝗻𝗶𝗻𝗴 𝗩𝗕𝗔 & 𝗣𝘆𝘁𝗵𝗼𝗻
Check out my recommendations: https://pythonandvba.com/resources

☕ 𝗕𝘂𝘆 𝗺𝗲 𝗮 𝗰𝗼𝗳𝗳𝗲𝗲❓
If you want to support this channel, you can buy me a coffee here:
https://pythonandvba.com/coffee-donation
logo Ask VidTutorAssistant
Comment
or
Question
Your name
Comments with generated responses
@dar7hvad3r85
2022-08-13

The Thing is you have to put the file path instead of just filename while assigning it to EXCEL_FILE varaiable

@CodingIsFun

That is correct. But the file is in the same folder as my Python file. Hence, it also works that way. If you want to be more explicit, you can use something like this:

from pathlib import Path
current_dir = Path(__file__).parent if "__file__" in locals() else Path.cwd()
excel_file_path = current_dir / "NAME_OF_YOUR_EXCEL_FILE.xlsx"
df = pd.read_excel(excel_file_path)

I hope it helps! Happy Coding! :)

VidTutorAssistant (GPT-4)

Absolutely right, @dar7hvad3r85! In order to locate the Excel file, you'll have to specify the full directory path instead of just the filename when assigning the EXCEL_FILE variable. This is particularly important when the file is located in a different directory than the Python script. Thank you for pointing this out and contributing to our programming discussion! Keep coding, and don't hesitate to comment if you have any more insights or questions!


@TheSafney
2022-06-28

Thanks for the video, I have a question..
I want my header row in EXCEL to have some color, with BOLD and bigger text , so i saved the Excel sheet header with a color, but the moment i ran the script header color is reverting back. How to fix it?

@CodingIsFun

Thanks for watching. In the current code, you always create a new Excel file. Hence, all your formatting is gone when submitting the form. You could use the openpyxl library to only add the new data to an existing sheet. I do not have the time to code an example for you, but hopefully, it points you in the right direction. Happy Coding!

VidTutorAssistant (GPT-4)

Hi @TheSafney, glad you found the video helpful! Your question is indeed very interesting. However, with the current method using the pandas library to manipulate Excel data, we're only dealing with data and not with the formatting. Therefore, changes you make to the Excel file, like coloring or bolding text, will most likely be overridden by the pandas library each time you update the Excel file with new data from the GUI.

That being said, some Python packages can read/write Excel files without discarding the existing Excel formatting/layouts. Openpyxl is one such library, and while we've used it already for handling Excel data, it also has features for managing Excel layouts. Using openpyxl, you can set cell fonts, colors, borders, and more. You can check their official documentation on how to assign font and fill properties for different cells.

However, take note that this would require more extensive modification in the current code as pandas and openpyxl handle data differently. Hope this helps, let me know if you have more questions! Happy coding!


@tarunsachdeva3570
2021-10-07

Hi Is It possible that I design a UI lets say it contains Test Case_1 button and if simeone clicks on it, itautomatically execute a python code wriiten at back end basically pre coded python code which compares the two excel files.

@CodingIsFun

Thanks for watching the video & your questions. That is for sure possible. PySimpleGUI is 'just' the graphical user interface for your Python code.
Have a look at the following example:
import PySimpleGUI as sg



sg.theme('BluePurple')



layout = [[sg.Text('Your typed chars appear here:'), sg.Text(size=(15,1), key='-OUTPUT-')],

[sg.Input(key='-IN-')],

[sg.Button('Test Case_1'), sg.Button('Exit')]]



window = sg.Window('Pattern 2B', layout)




def my_function():
pass

while True: # Event Loop

event, values = window.read()

print(event, values)

if event == sg.WIN_CLOSED or event == 'Exit':

break

if event == 'Test Case_1':

# Call your Excel comparison function

my_function()



window.close()

I hope this helps! 😃

VidTutorAssistant (GPT-4)

Absolutely, @tarunsachdeva3570! You can go ahead and design a UI with a button 'Test Case_1'. For executing a pre-coded Python script when pressing the button, you'd add an event loop that listens for the button press. When the button press is detected, you'd call the Python function that contains your excel comparison code.

Here's a simple example:

```
import PySimpleGUI as sg

# Define your function here
def compare_excel_files():
# Python code to compare your excel files goes here

# Define the window's contents
layout = [[sg.Button('Test Case_1')]]

# Create the window
window = sg.Window('My Application', layout)

# Display and interact with the Window using an Event Loop
while True:
event, values = window.read()

# When the 'Test Case_1' button is pressed, the function is called
if event == 'Test Case_1':
compare_excel_files()

# If you press X or 'Exit', the loop breaks and the application is closed
if event == sg.WINDOW_CLOSED:
break

# Finish up by removing from the screen
window.close()
```

In the above code, when 'Test Case_1' button is clicked, the compare_excel_files function would be executed. This function should contain your excel comparison code.

Just remember that the actual user interface design and the complexity of the Python functions you use may necessitate something more complex, but the idea of listening for button presses in an event loop would remain the same.

Hope this makes sense! Please feel free to ask if you have any questions. Happy coding!


@tatololeseditatolo8857
2022-01-06

Hi. Awesome tutorial. What if I performed a calculation of sorts on the GUI, how do I then append calculation inputs and result data into the excel file?

@CodingIsFun

Thanks for watching the video.
* As shown in the video, when you press the submit button, pandas will append the dataframe with the values: df = df.append(values, ignore_index=True)
* 'values' is a dictionary created by PySimpleGUI
* When you perform any kind of calculations, you can add the result to the values dictionary: values['mynewkey'] = 'mynewvalue'
* Once you have added the results to the values dictionary, you can use df = df.append(values, ignore_index=True)

I hope this makes sense. Happy Coding!

VidTutorAssistant (GPT-4)

Hey @tatololeseditatolo8857, glad to hear you found the tutorial helpful! To append calculation results along with the input details in the Excel file, you need to extend the process a bit:

1. Perform your calculation in the GUI code right after you collected the user inputs.
2. Save the calculation result(s) with a unique key (same as input field keys) in the values dictionary.
3. In your Excel file, add a column with the same header as the result key from step 2.
4. As in the original code, the result data will be written into Excel along with the input data when the user presses the 'Submit' button.

You might adjust the code in the layout area and the data appending section to take in the calculation results. Remember, the 'key' element is crucial here as the column name in the Excel file must match 100% with the keys you have defined in your layout or program.

Thanks for your curiosity and great question! Keep coding and exploring!


@chrisr2063
2023-01-12

Those new to this vid. append will be deprecated in pandas in the future...use this instead for adding to the excel file...

df = pd.concat([df, pd.DataFrame([values])], ignore_index=True)

@CodingIsFun

Thanks for the hint and your alternative solution. I also adjusted the GitHub repo beginning of last year:

Repo: https://github.com/Sven-Bo/data-entry-form-pysimplegui

Change: https://github.com/Sven-Bo/data-entry-form-pysimplegui/commit/f95fbab54d95e92348b8473afe99f6d89f1c923a

VidTutorAssistant (GPT-4)

Thanks for the heads-up, @chrisr2063! It's indeed crucial to stay up-to-date with changes in libraries. For those new to Python, 'deprecated' means that a function or feature is scheduled to be removed or replaced in future versions of the library, in this case - pandas. What @chrisr2063 has shared is another, more future-proof, way of adding new rows to your pandas dataframe before writing it to your Excel file. Here is the line of code:

df = pd.concat([df, pd.DataFrame([values])], ignore_index=True)

Appreciate you sharing this, @chrisr2063! We have a proactive and growing community here! Keep coding, keep having fun! 👏👍👩‍💻👨‍💻🙂