Skip to content

Instantly share code, notes, and snippets.

@ErickWendel
Last active September 3, 2015 01:22
Show Gist options
  • Save ErickWendel/555e4f8ab75da3985685 to your computer and use it in GitHub Desktop.
Save ErickWendel/555e4f8ab75da3985685 to your computer and use it in GitHub Desktop.
#http://www.sitepoint.com/using-python-parse-spreadsheet-data/
import xlrd
import json
#spreadsheets in my excel
investments = ['NTN-B Principal 150519', 'NTN-B Principal 150824', 'NTN-B Principal 150535']
#my excel
workbook = xlrd.open_workbook('pl.xls')
#get all names of spreadsheets for excel
sheets = [x for x in workbook.sheet_names()]
_objs = []
for sheet in sheets:
if sheet in investments:
worksheet = workbook.sheet_by_name(sheet)
sheets = {'sheet': sheet, 'data': []}
print('------------------')
print(sheet)
cells = [x for x in worksheet._cell_values]
for cell in range(0, len(cells)):
# print(cell)
if cell >= 2:
celula = cells[cell]
data = celula[0]
taxa_compra = celula[1]
taxa_venda = celula[2]
preco_compra = celula[3]
preco_venda = celula[4]
print('\n data: %s, \n taxa_compra : %s, \n taxa_venda: %s, \n preco_compra: %s, \n preco_venda:%s' % (data, taxa_compra, taxa_venda, preco_compra, preco_venda))
sheets['data'].append({
"data": data,
"taxa_compra" : taxa_compra,
"taxa_venda": taxa_venda,
"preco_compra": preco_compra,
"preco_venda": preco_venda
})
_objs.append(sheets)
with open ('items.json', 'w') as file:
file.write(json.dumps(_objs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment