FAQ
Is there a way to save a .xls file (the first worksheet) as a .dbf
or .csv without opening an instance of Excel with win32com.client
(been awhile, is this the best module these days for v2.5)? In case a
computer does not have Excel (2007) installed.

Search Discussions

  • Tim Chase at Jun 6, 2010 at 1:31 am

    On 06/05/2010 06:47 PM, noydb wrote:
    Is there a way to save a .xls file (the first worksheet) as a .dbf
    or .csv without opening an instance of Excel with win32com.client
    (been awhile, is this the best module these days for v2.5)? In case a
    computer does not have Excel (2007) installed.
    Use the "xlrd" module[1]

    ############################
    import csv
    import xlrd

    FILE_NAME = 'example.xls'
    wb = xlrd.open_workbook(FILE_NAME)

    for name in wb.sheet_names():
    out = file('%s.csv' % name, 'wb')
    writer = csv.writer(out)
    sheet = wb.sheet_by_name(name)
    for row in xrange(sheet.nrows):
    writer.writerow([
    sheet.cell_value(row, col)
    for col in xrange(sheet.ncols)
    ])
    out.close()
    #############################

    You say you only want the first sheet, so adjust accordingly.

    -tkc

    [1]
    http://pypi.python.org/pypi/xlrd/
  • Noydb at Jun 6, 2010 at 11:59 pm

    On Jun 5, 9:31?pm, Tim Chase wrote:
    On 06/05/2010 06:47 PM, noydb wrote:

    Is there a way to save a .xls file (the first worksheet) as a .dbf
    or .csv without opening an instance of Excel with win32com.client
    (been awhile, is this the best module these days for v2.5)? ?In case a
    computer does not have Excel (2007) installed.
    Use the "xlrd" module[1]

    ############################
    import csv
    import xlrd

    FILE_NAME = 'example.xls'
    wb = xlrd.open_workbook(FILE_NAME)

    for name in wb.sheet_names():
    ? ?out = file('%s.csv' % name, 'wb')
    ? ?writer = csv.writer(out)
    ? ?sheet = wb.sheet_by_name(name)
    ? ?for row in xrange(sheet.nrows):
    ? ? ?writer.writerow([
    ? ? ? ?sheet.cell_value(row, col)
    ? ? ? ?for col in xrange(sheet.ncols)
    ? ? ? ?])
    ? ?out.close()
    #############################

    You say you only want the first sheet, so adjust accordingly.

    -tkc

    [1]http://pypi.python.org/pypi/xlrd/
    Many thanks Tim, this worked well!

    In the interest of learning, anyone have a XLS to DBF solution?
  • Tim Chase at Jun 7, 2010 at 1:47 am

    On 06/06/2010 06:59 PM, noydb wrote:
    On Jun 5, 9:31 pm, Tim Chasewrote:
    [1]http://pypi.python.org/pypi/xlrd/
    Many thanks Tim, this worked well!

    In the interest of learning, anyone have a XLS to DBF solution?
    This becomes considerably trickier unless you're willing to have
    all your DBF fields be CHAR-format rather than their actual
    data-type. In an Excel file, one column can hold multiple
    data-types (text, numbers, formulas, dates, currency, etc)
    whereas (from my humble understanding) a single column in a DBF
    must be of the same data-type.

    That said, if you want to attempt it, I'd suggest using

    http://dbfpy.sourceforge.net/

    to create the DBF files using code similar to my CSV version
    (which mostly iterates over the Excel workbook and dumps the
    contents to CSV files -- just change it to dump the contents to
    your DBF).

    -tkc

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 5, '10 at 11:47p
activeJun 7, '10 at 1:47a
posts4
users2
websitepython.org

2 users in discussion

Tim Chase: 2 posts Noydb: 2 posts

People

Translate

site design / logo © 2022 Grokbase