Skip to content

Instantly share code, notes, and snippets.

@cyb3rsalih
Created July 23, 2024 14:05
Show Gist options
  • Save cyb3rsalih/d9c06486fa75f0836b8ac70123fd688f to your computer and use it in GitHub Desktop.
Save cyb3rsalih/d9c06486fa75f0836b8ac70123fd688f to your computer and use it in GitHub Desktop.
Split excel file with headers
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment