Skip to content

Instantly share code, notes, and snippets.

@sebkouba
Created November 5, 2015 12:36
Show Gist options
  • Save sebkouba/8564ec713bce206272c0 to your computer and use it in GitHub Desktop.
Save sebkouba/8564ec713bce206272c0 to your computer and use it in GitHub Desktop.
concatenate all xls files in directory tree
import os
import shutil
import xlrd
import xlwt
rootdir = os.getcwd()
wkbk = xlwt.Workbook()
outsheet = wkbk.add_sheet('Sheet1')
outrow_idx = 0
for dirpath, subdirnames, filenames in os.walk(rootdir):
for filename in filenames:
print "path: " + dirpath + " \\ " + filename
if filename.endswith("_Report.xls"):
current_file = os.path.join(dirpath, filename)
thisbook = xlrd.open_workbook(current_file)
insheet = xlrd.open_workbook(current_file).sheets()[0]
# not reading first line of each file
for row_idx in range(1, insheet.nrows):
for col_idx in xrange(insheet.ncols):
outsheet.write(outrow_idx, col_idx,
insheet.cell_value(row_idx, col_idx))
outrow_idx += 1
wkbk.save(r'C:\combined.xls')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment