Saving *.dbf as *.xls using Python?

Like whuber says, you have to write out the headers explicitly. I loaded up dbfpy and xlwt in a virtualenv and ran this:

from xlwt import Workbook, easyxf
import dbfpy.dbf
from time import time

def test1():
    dbf = dbfpy.dbf.Dbf("pipelines.dbf", readOnly = True)

    header_style = easyxf('font: name Arial, bold True, height 200;')

    book = Workbook()
    sheet1 = book.add_sheet('Sheet 1')

    for (i, name) in enumerate(dbf.fieldNames):
        sheet1.write(0, i, name, header_style)

    for (i, thecol) in enumerate(dbf.fieldDefs):
        name, thetype, thelen, thedec = str(thecol).split()
        colwidth = max(len(name), int(thelen))
        sheet1.col(i).width = colwidth * 310

    for row in range(1,len(dbf)):
        for col in range(len(dbf.fieldNames)):
            sheet1.row(row).write(col, dbf[row][col])

    book.save("pipelines-xls.xls")

if __name__ == "__main__":
    start = time()
    test1()
    end = time()
    print end - start

This gives me headers in my xls:

enter image description here


At ArcGIS 10.2 for Desktop a new tool called Table To Excel (Conversion) was introduced to export a table to an Excel file.

Summary

Converts a table to a Microsoft Excel file.

Usage

• Table To Excel is able to convert only to Microsoft Excel 5.0/95 Workbook (.xls) format.

Consequently, your Python code can now sometimes be as simple as:

arcpy.TableToExcel_conversion("C:/temp/SumStats.dbf","C:/temp/test.xls","NAME","CODE")