Skip to content

Instantly share code, notes, and snippets.

@rppowell-lasfs
Created January 23, 2018 06:28
Show Gist options
  • Save rppowell-lasfs/97e04bd0027051a4795037ac32140c67 to your computer and use it in GitHub Desktop.
Save rppowell-lasfs/97e04bd0027051a4795037ac32140c67 to your computer and use it in GitHub Desktop.
Python script to dumping dbf database files
# -*- coding: utf-8 -*-
"""
This python script is used for dumping dbf database files
* python2.7
"""
import dbfpy.dbf
import pprint
import csv
import xlwt
import datetime
def dump_dbf(filename):
print(filename)
db = dbfpy.dbf.Dbf(filename)
txt_o = open(filename+'_dump.txt', 'w+')
csv_file = open(filename+'.csv', 'wb')
csv_writer = csv.writer(csv_file)
csv_writer.writerow(sorted(db.fieldNames))
xls_workbook = xlwt.Workbook()
xls_worksheet1 = xls_workbook.add_sheet('Sheet 1')
xls_date_format = xlwt.XFStyle()
xls_date_format.num_format_str = 'M/D/YY'
print('- HEADER -----------------------------------------------------------------------')
print(sorted(db.fieldNames))
txt_o.write(pprint.pformat(sorted(db.fieldNames))+"\n")
csv_writer.writerow(sorted(db.fieldNames))
for i, value in enumerate(sorted(db.fieldNames)):
print i, value
xls_worksheet1.row(0).write(i,value)
print('- BODY -------------------------------------------------------------------------')
data = []
for row_count, row in enumerate(db):
print([(k,row[k]) for k in sorted(db.fieldNames)])
data.append(dict((k, row[k]) for k in db.fieldNames))
csv_writer.writerow([str(row[k]) for k in sorted(db.fieldNames)])
for column_count, value in enumerate(sorted(db.fieldNames)):
if isinstance(row[value], datetime.date):
xls_worksheet1.row(row_count+1).write(column_count,row[value], xls_date_format)
else:
xls_worksheet1.row(row_count+1).write(column_count,row[value])
#print(pprint.pformat(data))
txt_o.write(pprint.pformat(data)+"\n")
xls_workbook.save(filename+'.xls')
print('--------------------------------------------------------------------------------')
if __name__ == '__main__':
#dump_dbf('classes.dbf')
#dump_dbf('master.dbf')
#dump_dbf('members.dbf')
dump_dbf('regconfi.dbf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment