i have got a list of excel spread sheets that i want to convert in to .dbf and save them with the same name. can someone tell me how to could you use the sheet name only from the whole path in spreadsheet list, thanks

3817
13
Jump to solution
08-26-2015 07:41 AM
JKBellamkonda
New Contributor III
0 Kudos
1 Solution

Accepted Solutions
RebeccaStrauch__GISP
MVP Emeritus

Something like this will work, and I can see the dbf in Catalog, but couldn't open in Excel.  It may be do to the name and/or column lengths I had in my sample.  you would need to play with that.  But it depends on what you are are planning on using the .dbf files for.

import arcpy
import os
theWorkspace = r"c:\__temp"
arcpy.env.workspace = theWorkspace  
sheets = ["BasinReserve", "DowseToPetone", "ManaParemta"]
table = "anExcel.xlsx"
table = os.path.join(theWorkspace, table)
print table
for sheet in sheets:
  inputTable = table + "\\" + sheet + "$"
  print inputTable
  arcpy.TableToTable_conversion(inputTable, theWorkspace, sheet )

View solution in original post

13 Replies
JustinJohnson2
New Contributor III

If you just need to convert the filename from xls to dbf, this is how I did something similar in a recent project.

It recursively searches a directory for any files with the extension you want (in this case ".xls"), then generates a filename in an output directory with the other extension (".pdf").

import os.path

rootdir = r'C:\projects'
outpath = r'C:\output'

for (dirName, subdirList, fileList) in os.walk(rootdir):
    for filename in fileList:
        if os.path.splitext(filename)[1].lower() == ".xls":
            
            pdffilename = os.path.join(outpath, filename[:-3] + "pdf")
            // do your conversion here
            
            print pdffilename
0 Kudos
DarrenWiens2
MVP Honored Contributor

Can you please: 1.) post your code directly (help here) so we can help without typing out your example, and 2.) provide exactly what 'print project' produces? It appears you have two different lists: "spreadsheet" and "spreadsheet_list". As it is written, we need to know what's in "spreadsheet".

0 Kudos
JKBellamkonda
New Contributor III

Hi Darren this is what i am trying to do, add all the information from spreadsheets to a feature class by joining tables copying information and removing joincode1.PNGcode2.PNG

0 Kudos
DarrenWiens2
MVP Honored Contributor

The point of my post was that I don't particularly care what your script does, I just wanted to help without having to type out the relevant parts of your script. By posting your code directly into a code block, we can copy/paste it into our response - we can't do that with a screenshot.

In any case, it looks like Rebecca has gone to the trouble of typing out a functioning solution, so I'd go with that.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

What Darren mean is, check out Posting Code blocks in the new GeoNet​   instead of just a screen shot.  Can't help as much without the code.

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Something like this will work, and I can see the dbf in Catalog, but couldn't open in Excel.  It may be do to the name and/or column lengths I had in my sample.  you would need to play with that.  But it depends on what you are are planning on using the .dbf files for.

import arcpy
import os
theWorkspace = r"c:\__temp"
arcpy.env.workspace = theWorkspace  
sheets = ["BasinReserve", "DowseToPetone", "ManaParemta"]
table = "anExcel.xlsx"
table = os.path.join(theWorkspace, table)
print table
for sheet in sheets:
  inputTable = table + "\\" + sheet + "$"
  print inputTable
  arcpy.TableToTable_conversion(inputTable, theWorkspace, sheet )
JKBellamkonda
New Contributor III

Thanks Rebecca, this piece of code took me a while to get it but was helpful. Cheers

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

So were you able to get it to do what you needed?    I haven't really dug into your code, but it sounds like you are joining a bunch of tables so you can copy attributes to one of them from the other?  Have you looked at

Join Field—Help | ArcGIS for Desktop  ??  That allows you to join (permanently) to tables based on a in_field  matching join_field, and you can select which of the fields you want from the join_table.

Sample from help:

# Purpose: Join two fields from a table to a feature class 

# Import system modules
import arcpy
from arcpy import env

# Set the current workspace 
env.workspace = "c:/data/data.gdb"

# Set the local parameters
inFeatures = "zion_park"
joinField = "zonecode"
joinTable = "zion_zoning"
fieldList = ["land_use", "land_cover"]

# Join two feature classes by the zonecode field and only carry 
# over the land use and land cover fields
arcpy.JoinField_management (inFeatures, joinField, joinTable, joinField, fieldList)

That might be what you are trying to do?

0 Kudos
JKBellamkonda
New Contributor III

Yeah i got that working. Join field dint really seem helpful because of the long processing time and the number of fields i have to deal with.

0 Kudos