Leggere file Excel con Python e xlrd
La libreria xlrd è una delle tante con cui è possibile leggere file Excel con Python.
Supporta sia XLS che XLSX.
Nel complesso non è tra le mie favorite, però ammetto che è molto veloce nel leggere i files (o almeno mi ha dato questa impressione).
Per installarla possiamo usare pip:
(venv) $ pip3 install xlrd
Io sto usando un ambiente virtuale.
Qui sotto un esempio abbastanza completo su come leggere un file:
import xlrd
from xlrd.timemachine import xrange
doc = xlrd.open_workbook('test.xls')
print("NUMERO DI FOGLI PRESENTI")
print(doc.sheet_names())
print("------------")
sheet = doc.sheet_by_index(0)
print("NUMERO DI RIGHE NEL FOGLIO 0")
print(sheet.nrows)
print("NUMERO DI COLONNE NEL FOGLIO 0")
print(sheet.ncols)
print("------------")
print("ITERIAMO SU TUTTE LE RIGHE E COLONNE DEL FOGLIO 0, PARTENDO DALLA RIGA 1")
for row in xrange(1, sheet.nrows):
for col in xrange(0, sheet.ncols):
cell = sheet.cell(row, col)
print('Column: [%s] cell_obj: [%s]' % (col, cell))
Il codice non è difficile ed è commentato.
Enjoy!
python pip excel xlrd xls xlsx
Commentami!