Created
September 13, 2017 07:38
-
-
Save walterrenner/318ea851a312a9cb1620655a5064aafb to your computer and use it in GitHub Desktop.
Split large Excel files into chunks of n
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import xlrd | |
import xlwt | |
def chunks(l, n): | |
"""Yield successive n-sized chunks from l.""" | |
for i in range(0, len(l), n): | |
yield l[i:i + n] | |
def split_excel(infile, chunk_size=100): | |
workbook = xlrd.open_workbook(infile) | |
sheet = workbook.sheet_by_index(0) | |
_chunks = list(chunks(range(0, sheet.nrows), chunk_size)) | |
for _chunk in _chunks: | |
wb = xlwt.Workbook() | |
ws = wb.add_sheet('Sheet 1') | |
rows = [sheet.row_values(row, 0) for row in _chunk] | |
for rowx, row_values in enumerate(rows): | |
for colx, cell_value in enumerate(row_values): | |
ws.write(rowx, colx, cell_value) | |
wb.save('output_' + str(_chunks.index(_chunk)) + '.xls') | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i', '--input', help='Input file name', required=True) | |
args = parser.parse_args() | |
split_excel(args.input, chunk_size=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment