Skip to content

Instantly share code, notes, and snippets.

@naitsric
Created February 7, 2016 21:57
Show Gist options
  • Save naitsric/fe0a4a28833d6ef38708 to your computer and use it in GitHub Desktop.
Save naitsric/fe0a4a28833d6ef38708 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import os, sys
import re
from stat import *
import xlrd, xlwt
from datetime import datetime
def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname,f,top)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname
def visitfile(fullname,file,path):
global g_row
if "xls".lower() in str(file).lower() and "~$" not in str(file).lower() and "anulada" not in str(file).lower():
# print 'visiting', file, 'in', path, "=", fullname
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on', num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
try:
workbook = xlrd.open_workbook(fullname)
worksheet = workbook.sheet_by_index(0)
num_rows = worksheet.nrows
fecha = worksheet.cell_value(5, 2)
a1_tuple = xlrd.xldate_as_tuple(fecha, workbook.datemode)
a1_datetime = datetime(*a1_tuple)
for row in range(13, num_rows):
articulo = worksheet.cell_value(row,3)
cantidad = worksheet.cell_value(row,5)
v_unit = worksheet.cell_value(row,6)
v_total = worksheet.cell_value(row,7)
if len(articulo)==0:
break
ws.write(g_row, 0, articulo, style0)
ws.write(g_row, 1, cantidad, style0)
ws.write(g_row, 2, v_unit, style0)
ws.write(g_row, 3, v_total, style0)
ws.write(g_row, 4, a1_datetime, style1)
ws.write(g_row, 5, str(file.replace("ORDENES DE COMPRA", "").replace(".xlsx", "").replace(".xls", "").replace(str(re.sub(r'[^\d]', ' ', file.replace("ORDENES DE COMPRA", "").replace(".xlsx", "").replace(".xls", ""))).strip(), "")).strip(), style1)
ws.write(g_row, 6, str(re.sub(r'[^\d]', ' ', file.replace("ORDENES DE COMPRA", "").replace(".xlsx", "").replace(".xls", ""))).strip(), style1)
g_row+=1
print file.replace("ORDENES DE COMPRA", "").replace(".xlsx", "").replace(".xls", "").replace(str(re.sub(r'[^\d]', ' ', file.replace("ORDENES DE COMPRA", "").replace(".xlsx", "").replace(".xls", ""))).strip(), "")
print a1_datetime
except Exception, e:
print 'visiting', file, 'in', path, "=", fullname
print e
if __name__ == '__main__':
global g_row
g_row = 1
wb = xlwt.Workbook(encoding='latin-1')
ws = wb.add_sheet('A Test Sheet')
walktree(sys.argv[1], visitfile)
wb.save('resultado-{0}.xls'.format(datetime.now()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment