Questo modulo fornisce alcune classi per facilitare l’importazione di dati da file excel e csv Si suppone che ogni file sia composto da fogli Sheet
Un esempio di importazione sarà:
from jmb.organization.models import Contact
from jmb.core.utils.data_import import XlsFile
class Anagrafiche(XlsFile):
fields_map = {
'code' : 'ancodice',
'description' : 'andescri',
'foo' : 'antipcon',
}
def do(self, row, kw):
# modi alternativi di referenziare i dati
Contact.objects.get_or_create(code=row.ancodice, defaults=kw)
#Contact.objects.get_or_create(code=kw['code'], defaults=kw)
x = Anagrafiche('anagrafiche.xls', auto=True)
x.read()
un metodo semplice di dare il nome alle colonne è di impostarlo con l’attributo Sheet.field_list, le colonne possono poi essere mappate in field names tramite Sheet.fields_map
Abstract sheet
implement here your import
Parameters: |
|
---|
a list of field names. Optional. When present it will be used to name each column instead of looking at the column header. Field name here is the row attribute (eg: row.first_name)
Parameters: |
|
---|
Parameters: |
|
---|
Abstract sheet
Clean each column heading to make it a suitable field_name. By default strip empty spaces, non ascii chars, parenthesis :param value: the column header
implement here your import
Parameters: |
|
---|
a list of field names. Optional. When present it will be used to name each column instead of looking at the column header. Field name here is the row attribute (eg: row.first_name)
a dict mapping field names to column headers, as an example, if field_name has {‘first_name’ : ‘Nome’} it means that read() will set:
kw['first_name'] = row.Nome
Return a dict of all values of a (possible) fk as dict
Parameters: |
|
---|
Fields listed in this list will be set None if empty specifically needed to prevent unique problems
Loop on all rows of a sheet
Parameters: |
|
---|