6

I've been let loose in the workplace to learn python to do things in Arcmap 10. so, I am learning python as I go and trying to remember the programming I have done.

Where I am in this project is converting a dbf, or csv, xls in a simple fashion. from there, all the files will be copied together into one file. I've got the all-in-one xls working, but I can't find an easy, simple dbf to xls solution.

I condensed code found here: http://blog.gmane.org/gmane.comp.python.education/page=12

into:

from xlwt import Workbook
import dbfpy.dbf
input0 = '...file.dbf'
output = '...file.xls'
def test1():
 dbf = dbfpy.dbf.Dbf(input0)
 book = Workbook()
 sheet1 = book.add_sheet('Sheet 1')
 for row in range(len(dbf)):
 for col in range(2):#chop to two for my purposes, gm 
 sheet1.row(row).write(col, dbf[row][col])
 book.save(output)
test1()

This works, minus the lack of field names.

Hornbydd
44.9k5 gold badges43 silver badges84 bronze badges
asked May 9, 2012 at 17:58
7
  • 2
    Given that Excel opens DBF files directly, without loss of information, why do you need to perform a conversion? Commented May 9, 2012 at 18:04
  • you use arcmap lately? the quickexport_interop doesn't support xls or xlsx. dbfpy one does one sheet (i think). csv only has one sheet, as well. I'm tryin to summarize many files at once, some with 10k+ rows. Within arcmap with the file open, this takes way too long. Commented May 9, 2012 at 18:12
  • to clear things up: yes, i know that excel will open dbf's and csv's. if i can get QuickExport results into xls, i know how to merge them by column into one file. i'll take suggestions for merging dbf's, but i'll still need to have it end up in xls. once done with my end, the file will get all pretty'd up, formatted and sent out to clients, managers, etc. Commented May 9, 2012 at 18:24
  • So what exactly is the problem with your posted snippet? Your dbf headers aren't coming through to Excel? Commented May 9, 2012 at 19:32
  • 1
    That's because you need to write a row of field names: your code only writes the data records one by one. Commented May 9, 2012 at 19:49

3 Answers 3

9

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

answered May 9, 2012 at 20:48
4
  • thank you! i was monkeying around with things, but for (i, name) in enumerate(dbf.fieldNames): sheet1.write(0, i, name) was what i was attempting with much fail. can't upvote, sorry. Commented May 9, 2012 at 21:22
  • @gm70560 - If this indeed answered the question, please mark it as answered. Commented May 10, 2012 at 13:56
  • @ Chad Cooper- there's your upvote. Commented Aug 8, 2012 at 2:59
  • 2
    @ChadCooper - Thanks for the code, it was very helpful. A quick note... I realized that at least for me your final for loop was skipping the first row of data it was supposed to have when writing the xls. I believe this is because the headers in the dbf are above the 0 row, while in the xls they become the 0 row. I changed the range to start at 0 and did "sheet1.row(row + 1) which makes it write to row 1 but still starts reading the dbf from row 0. Hope this helps others! Commented Oct 6, 2012 at 11:40
2

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")
answered Jun 1, 2014 at 22:29
0

Export an Excel table to .dbf format in ArcGIS.

In Excel 2007, save the table as, 'Excel 97-2003 Workbook' (.xls format).
Navigate to the .xls table location through ArcCatalog, and expand the file to view the worksheets.
Right-click on the worksheet representing the table. Select Export> To dBase (single).

SaultDon
10.5k1 gold badge46 silver badges79 bronze badges
answered May 31, 2014 at 17:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.