Skip to content

Instantly share code, notes, and snippets.

@milmazz
Last active Dec 11, 2015
Embed
What would you like to do?
Merge info from two excel files, after that, create another excel file with the results.
#!/usr/bin/python
# -*- coding: utf-8 -*-
__version__ = '0.1'
import xlrd
import re
import xlwt
import argparse
# Parameters
parser = argparse.ArgumentParser()
parser.add_argument("start", type=int, help="Initial row")
parser.add_argument("end", type=int, help="End row")
args = parser.parse_args()
# Compras book
# FIXME: Enable parameters
book = xlrd.open_workbook('compras.xls')
# TX y TNG sheet
sheet = book.sheet_by_index(3)
# FIXME: solppos initial value
solppos = []
pattern = re.compile("^P.*==\s*(?P<solppos>\d+)")
# FIXME: Enable parameters
for row in xrange(args.start - 1, args.end - 1):
# We take the value(s) of SOLPPOS (the value after == symbol)
col_g = 6
cell_values = sheet.cell_value(row, col_g).split('\n')
for cell_value in cell_values:
match = re.match(pattern, cell_value)
if match:
solppos.append(match.group('solppos'))
# Compras sheet
sheet = book.sheet_by_index(2)
# FIXME: solp initial value
solp = ''
nps = {}
for rownum in xrange(2, sheet.nrows):
status = sheet.cell(rownum, 0).value
# From the spec
# https://secure.simplistix.co.uk/svn/xlrd/trunk/xlrd/doc/xlrd.html#sheet.Cell-class
solp_cell = sheet.cell(rownum, 2)
np_cell = sheet.cell(rownum, 3)
if (solp_cell.ctype == 2 or solp_cell.ctype == 3) and (np_cell.ctype == 2 or np_cell.ctype == 3):
solp = str(int(solp_cell.value))
np = str(int(np_cell.value) / 10000)
np_pos = int(np_cell.value) % 10000
if solp in solppos and (status == "4RP" or status == "5MA" or status == "6MR"):
if nps.has_key(np):
nps[np].append(np_pos)
else:
nps[np] = [np_pos,]
# Almacen book
# FIXME: Enable parameters
book = xlrd.open_workbook('almacen.xls')
# NAVE 1 sheet
sheet = book.sheet_by_index(0)
# Results
w = xlwt.Workbook()
ws1 = w.add_sheet(u'Results')
row_num = 0
for rownum in xrange(5, sheet.nrows):
np_cell = sheet.cell(rownum, 4)
if np_cell.ctype == 2 or np_cell.ctype == 3:
np = str(int(np_cell.value))
if np in nps.keys():
np_pos = sheet.cell(rownum, 5).value
if np_pos in nps[np]:
rows = sheet.row_values(rownum, end_colx=16)
for col_num in range(len(rows)):
ws1.write(row_num, col_num, rows[col_num])
row_num = row_num + 1
w.save('results.xls')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment