Python

import os
import pandas as pd

def convert_excel_to_csv(folder_path):
# Traverse through folders and subfolders
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.endswith((“.xlsx”, “.xls”)): # Detec…


This content originally appeared on DEV Community and was authored by RUDRA SHARMA

import os
import pandas as pd

def convert_excel_to_csv(folder_path):
    # Traverse through folders and subfolders
    for root, dirs, files in os.walk(folder_path):
        for file in files:
            if file.endswith((".xlsx", ".xls")):  # Detect Excel files
                excel_path = os.path.join(root, file)
                try:
                    print(f"Processing {excel_path}")
                    # Load the Excel file
                    excel_file = pd.ExcelFile(excel_path)

                    # Iterate through each sheet
                    for sheet_name in excel_file.sheet_names:
                        # Read the sheet into a DataFrame
                        df = excel_file.parse(sheet_name)

                        # Define the CSV filename
                        csv_filename = f"{os.path.splitext(file)[0]}_{sheet_name}.csv"
                        csv_path = os.path.join(root, csv_filename)  # Save CSV in the same directory as the Excel file

                        # Save the DataFrame as a CSV file
                        df.to_csv(csv_path, index=False)
                        print(f"Saved: {csv_path}")
                except Exception as e:
                    print(f"Error processing {excel_path}: {e}")

# Provide the directory path where the script should start
run_directory = os.getcwd()  # Use current working directory
convert_excel_to_csv(run_directory)


This content originally appeared on DEV Community and was authored by RUDRA SHARMA


Print Share Comment Cite Upload Translate Updates
APA

RUDRA SHARMA | Sciencx (2025-04-11T00:27:51+00:00) Python. Retrieved from https://www.scien.cx/2025/04/11/python-3/

MLA
" » Python." RUDRA SHARMA | Sciencx - Friday April 11, 2025, https://www.scien.cx/2025/04/11/python-3/
HARVARD
RUDRA SHARMA | Sciencx Friday April 11, 2025 » Python., viewed ,<https://www.scien.cx/2025/04/11/python-3/>
VANCOUVER
RUDRA SHARMA | Sciencx - » Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/04/11/python-3/
CHICAGO
" » Python." RUDRA SHARMA | Sciencx - Accessed . https://www.scien.cx/2025/04/11/python-3/
IEEE
" » Python." RUDRA SHARMA | Sciencx [Online]. Available: https://www.scien.cx/2025/04/11/python-3/. [Accessed: ]
rf:citation
» Python | RUDRA SHARMA | Sciencx | https://www.scien.cx/2025/04/11/python-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.