Skip to content

Instantly share code, notes, and snippets.

@xythobuz
Created February 25, 2017 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xythobuz/8354f602b4f2fff4983089c00750ebb2 to your computer and use it in GitHub Desktop.
Save xythobuz/8354f602b4f2fff4983089c00750ebb2 to your computer and use it in GitHub Desktop.
Large excel file row excerpt utility
#!/usr/bin/env python
# This program takes two arguments, the input filename and the output filename.
# Both should be .xlsx files.
# It will then only copy each 50th row from the input to the output file.
skip = 50;
from openpyxl import load_workbook, Workbook
import sys
if len(sys.argv) < 3:
print("read and write filename required!")
quit()
rb = load_workbook(filename = sys.argv[1], read_only = True)
wb = Workbook(write_only = True)
print("Reading from sheet {}...".format(rb.sheetnames[0]))
rs = rb[rb.sheetnames[0]]
ws = wb.create_sheet()
count = skip - 1 # Ensure first row is copied
print("Iterating data...")
for row in rs.iter_rows():
count += 1
if count >= skip:
count = 0
thisrow = []
for col in row:
thisrow.append(col.value)
ws.append(thisrow)
print("Writing to file {}...".format(sys.argv[2]))
wb.save(sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment