Search code examples
pythonexcelopenpyxldbf

Transfer data from excel worksheet (openpyxl) to database table (dbf)


I have a simple problem of reading an excel worksheet, treat every row containing about 83 columns as unique database record, add it to local datum record and ultimately append and write onto DBF file.

I can extract all the values from excel and add them to the list. But the list is not correct syntax and I don't know how to prepare/convert the list to database record. I am using Openpyxl, dbf and python 3.7.

At the moment I am only testing and trying to prepare the data for Row 3 (hence min_max rows = 3)

I understand that the data should be in the format (('','','', ... 83 entries), \ ('','','', ... 83 entries) \ )

But I do not know how to convert the list data into record or, alternatively, how to read in excel data directly into a DF appendable format

tbl_tst.open(mode=dbf.READ_WRITE) # all fields character string

for everyrow in ws_IntMstDBF.iter_rows(min_row = 3, max_row = 3, max_col = ws_IntMstDBF.max_column-1):
    datum = [] #set([83]), will defining datum as () help solve the problem?
    for idx, cells in enumerate(everyrow):
        if cells.value is None: # for None entries, enter empty string
            datum.append("")
            continue
        datum.append(cells.value) # else enter cell values 

     tbl_tst.append(datum) # append that record to table !!! list is not record error here

tbl_tst.close()

The error is complaining about using list to append to table, and this should be a record etc. Please guide how I can convert excel rows into appendable DBF table data.

raise TypeError("data to append must be a tuple, dict, record, or template; not a %r" % type(data))
TypeError: data to append must be a tuple, dict, record, or template; not a <class 'list'>

Solution

  • Change

    tbl_tst.append(datum)
    

    to

    tbl_tst.append(tuple(datum))
    

    and that will get rid of that error. As long as all your cell data has the appropriate type then the append should work.