Skip to content

Instantly share code, notes, and snippets.

@cyb3rsalih
Created July 23, 2024 14:05
Show Gist options
  • Select an option

  • Save cyb3rsalih/d9c06486fa75f0836b8ac70123fd688f to your computer and use it in GitHub Desktop.

Select an option

Save cyb3rsalih/d9c06486fa75f0836b8ac70123fd688f to your computer and use it in GitHub Desktop.

Revisions

  1. cyb3rsalih created this gist Jul 23, 2024.
    26 changes: 26 additions & 0 deletions split_xlsx.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import pandas as pd


    # Function to split the DataFrame into smaller DataFrames
    def split_dataframe(df, chunk_size):
    return [df[i : i + chunk_size] for i in range(0, df.shape[0], chunk_size)]


    # Read the Excel file
    input_file = "input.xlsx"
    df = pd.read_excel(input_file)

    # Define the number of records per split file
    records_per_file = 1000 # Adjust as needed

    # Split the DataFrame
    chunks = split_dataframe(df, records_per_file)

    # Save each chunk to a new Excel file
    for i, chunk in enumerate(chunks):
    output_file = f"output_{i+1}.xlsx"
    chunk.to_excel(output_file, index=False)
    print(f"Saved {output_file}")


    # /w ChatGPT