Skip to content

Instantly share code, notes, and snippets.

@fabiomello
Last active August 16, 2023 17:21
Show Gist options
  • Select an option

  • Save fabiomello/061094f4f2020399ed359076bb338905 to your computer and use it in GitHub Desktop.

Select an option

Save fabiomello/061094f4f2020399ed359076bb338905 to your computer and use it in GitHub Desktop.
Split XLSX in Python
pip install pandas openpyxl
import pandas as pd

df = pd.read_excel("/tmp/file.xlsx")
rows_per_file = 1000
n_chunks = len(df) // rows_per_file

for i in range(n_chunks):
  start = i*rows_per_file
  stop = (i+1) * rows_per_file
  sub_df = df.iloc[start:stop]
  sub_df.to_excel(f"/tmp/splited-output-{i}.xlsx", sheet_name="a")
if stop < len(df):
  sub_df = df.iloc[stop:]
  sub_df.to_excel(f"/tmp/splited-output-{i}.xlsx", sheet_name="a")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment