Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Last active September 4, 2015 06:35
Show Gist options
  • Save santiago-salas-v/5deda119fec964afe94f to your computer and use it in GitHub Desktop.
Save santiago-salas-v/5deda119fec964afe94f to your computer and use it in GitHub Desktop.
def run_concatenation(directory=None):
import re, os
from xlwings import Workbook, Range, Application
wb = Workbook(app_visible=False)
lastRow = 3
newlastRow = lastRow
lastColumn = 6
if directory is None:
directory = os.getcwd()
for file in os.listdir(directory):
findling = re.search(r'.*Data.*[0-9]{2,3}.*.xls$', file)
if findling:
wb2 = Workbook(os.path.abspath(findling.group()), \
app_visible=False)
addedRows = Range((4, 1), wkb = wb2).table.last_cell.row - 3
newlastRow = lastRow + addedRows
Range((lastRow+1, lastColumn+1), wkb = wb).value = \
findling.group()
Range((lastRow+1, 1), wkb = wb).table.value = \
Range((4, 1), (newlastRow, lastColumn), wkb = wb2).value
lastRow = newlastRow
wb2.close()
wb.save(os.path.join(os.getcwd(), 'Output.xlsx'))
wb.close()
run_concatenation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment