Python — Convert file types — Streamlit
In this article, I’ll walk you through how I built a simple yet powerful tool to convert Excel files into JSON or CSV format or switch between CSV and JSON. This tool addresses a real problem I face frequently: needing data in multiple formats for testing and analysis. This article assumes that you are familiar with how Streamlit works. If not read through StreamLit Basic Concepts.
Here’s the project repository: file-type-cast-streamlit.
Check out the application here. This is another cool thing with Streamlit, when your application has no dependencies on databases or does not need any secrets, deploy it for Free…
The Problem
The issue started when I needed to work with data in different formats. I had a script that processed JSON data and needed it tested, but I only had the data in Excel format. At the same time, I also needed the same data in CSV format to upload it to the database for analysis.
Given the sensitive nature of the data, using online tools wasn’t an option. I had to write separate scripts to convert the Excel file into both JSON and CSV formats. While this worked, I realized this would become a recurring task that could be automated to save time and avoid errors. This might be a good idea for a fun project…
The Solution
While I was not able to build a solution that I could use extensively, when learning the Streamlit framework, I thought this would be a perfect use case to solve a real problem I face. Building a tool that could handle file format conversions — Excel to JSON, Excel to CSV, or between CSV and JSON — seemed like a great way to explore Streamlit’s capabilities while solving a practical issue.
With all that said, this project was built mainly for learning and fun. It allowed me to dive into Streamlit, apply it to a real problem, and see firsthand how easily I can develop a front end without putting much effort into it.
Core Logic
The file conversion is handled using the pandas library in Python. Pandas make it easy to work with DataFrames, and converting between CSV, JSON, and DataFrames is quick and simple.
Here’s the code to read from CSV, JSON, and Excel files:
import pandas as pd
df_csv = pd.read_csv("path/to/file.csv")
df_json = pd.read_json("path/to/file.json")
df_xlsx = pd.read_excel("path/to/file.xlsx")Once loaded, the data in these files will be represented as DataFrames, which are easy to work with. You can write the data back to CSV, JSON, or Excel files like this: Next, here’s how you can write the data back to CSV, JSON, or Excel files:
import pandas as pd
df.to_csv("path/to/file.csv")
df.to_json("path/to/file.json")
df.to_excel("path/to/file.xlsx")This logic is the same as the one I used in my scripts before I built the Streamlit tool. By introducing the frontend for this logic, I hope to have made the user experience better, making the process more accessible and intuitive without needing to manually run scripts.
Hero of the article — Streamlit
As simple as the core logic is, the frontend built around it using Streamlit is also quite simple and effective. Streamlit makes it easy to build interactive web apps with minimal code, which is exactly what I needed for this tool.
For a complete view of the code, feel free to check out the repository. Below, I’ll highlight some key Streamlit concepts that led me to build this tool:
file_uploader
Streamlit makes it easy to upload files with the file_uploader widget. Here’s the code to upload a file, with support for multiple file types:
import streamlit as st
uploaded_file = st.file_uploader("Choose a file",
type=["csv", "json", "xlsx"],
accept_multiple_files=False)
uploader_file_name = uploaded_file.nameThis is the code snippet that built the below — As simple as that
Once uploaded you can access the file from uploaded_file object and the uploaded file name is available through uploaded_file.name
Get Nishaanth S P’s stories in your inbox
Join Medium for free to get updates from this writer.
For more details, check out the Streamlit file_uploader Documentation
tabs
With Streamlit, you can easily create tabs to organize content. Here’s how I used tabs to manage different sections of the tool:
import streamlit as st
st.title("Tabs test")
tab1, tab2, tab3 = st.tabs(["Tab1", "Tab2", "Tab3"])
with tab1:
st.header("This is Tab 1")
with tab2:
st.header("You just landed on Tab 2")
with tab3:
st.header("Wow! you are at a tab which no other human reached")This code creates three tabs without needing any CSS or JavaScript — just Python 😌!
Anything inside the with block of each tab will be exclusive to that tab. Content outside the with blocks will be shared across all tabs.
For more details, check out the StreamLit tabs Documentation
download_button
The download_button widget allows you to provide a downloadable file to the user. Here’s how I used it to let users download the converted files in CSV or JSON format:
def get_download_button(df, type):
if type=="csv":
return st.download_button(
label="Download CSV",
data=df.to_csv().encode("utf-8"),
file_name=f"edited_data_{datetime.now():%Y-%m-%d_%H-%M-%S}.csv"
)
else:
return st.download_button(
label="Download JSON",
data=df.to_json(orient='records').encode("utf-8"),
file_name=f"edited_data_{datetime.now():%Y-%m-%d_%H-%M-%S}.json"
)This function generates a download button for the user to click, which will then download the respective file. You don’t need to worry about any extra logic to convert the data into a downloadable file — Streamlit handles it for you.
For more details, check out the Streamlit download_button Documentation
Conclusion
In this article, I showed you how I built a simple Streamlit tool to convert between CSV, JSON, and Excel formats — something I deal with all the time. The core logic was simple with Pandas, but Streamlit made it easy to turn this into an interactive app for uploading files, converting them, and downloading the results.
I highlighted some key Streamlit features like file_uploader, tabs, and download_button that made the process quick and smooth.
This project started as a fun learning exercise and turned into a tool I now use regularly. Streamlit is a great way to build quick internal tools, and I’m excited to keep using it for future projects.
And now, I’m off to create a Streamlit app that auto-generates excuses for my meetings.








