TableToExcel - Loop

4311
28
06-13-2017 01:24 PM
DougHaller1
New Contributor III

Hi GeoNet and thanks in advance. I'm new to Python. 

My goal is to write a script that converts all the shapefiles in a gdb into a csv (although I will take an Excel spreadsheet, too).  I plan to convert the final program into a script tool with parameters for which gdb to access and where to save the spreadsheets. I can get a single shapefile to convert into an Excel spreadsheet. I would like to loop through the gdb and convert each shapefile to a unique csv (for this case, let's assume that the gdb contains only shapefiles).

My code fails and I have received a couple of different error messages depending on the variable I use for the in_table argument (hope I have that right).  

I suspect that I am not correctly naming my in_table and out_xls variables.

Here's a screen shot of my code. Sorry for the small size, not sure how to enlarge the text.

tabletoexcel loop

Tags (2)
0 Kudos
28 Replies
BlakeTerhune
MVP Regular Contributor
Also I would recommend using the os module for working with filepaths(see os.path.join), its cleaner than using string backslashes and concatenating filenames together.

I agree. Dan Patterson wrote an insightful blog post on the topic.

/blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python 

RebeccaStrauch__GISP
MVP Emeritus

A few things, first this should be a question not a discussion if you are able to fix it,

done...I switched it to a question.

DougHaller1
New Contributor III

Thanks for the comments, Ian. I the future I will post to questions with questions.

As for the code edits (lines 26/31) I will try this.

I understand the advantage of os.path.join(). I will read the links you shared to learn more on how to use the OS module.

0 Kudos
ClintonDow1
Occasional Contributor II

I see your os.path and raise you a pathlib

DanPatterson_Retired
MVP Emeritus

Don't toy with them Clinton... then can't use 3.6.1 yet let alone 3.anything if they are still in ArcMap

ClintonDow1
Occasional Contributor II

It can still be pip installed for 2.7, its just standard lib in 3.4+ -- but you're right, for base 10.x os.path is the way to go! I'm stuck in the Pro mindset  

0 Kudos
DanPatterson_Retired
MVP Emeritus

For those planning ahead, the link in that topic is useful for comparing os and path equivalencies/similarities

https://docs.python.org/3.7/library/pathlib.html#correspondence-to-tools-in-the-os-module

DanPatterson_Retired
MVP Emeritus

For code formatting, it is best to past it in so it can be retrieved... Code Formatting the Basics ++ if people want to test

FC_Basson
MVP Regular Contributor

You can try this code, taking into account that Excel files are limited to 65,535 (XLS) or 1,048,576 (XLSX) rows:

import arcpy
arcpy.env.workspace = r'D:\MyfolderOrGDB'
fcs = arcpy.ListFeatureClasses()
for fc in fcs: 
  rows = int(arcpy.GetCount_management(fc).getOutput(0))
  if rows < 65535:
    desc = arcpy.Describe(fc)
    xlsfile = '{}{}{}'.format('D:/OutputFolder/', desc.name, '.xls')
    arcpy.TableToExcel_conversion(fc,xlsfile)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
DougHaller1
New Contributor III

Thanks FC

I will try your code. It looks like I might need to make some changes for it to work on my data.

  •  Line 2 - Do I need to change my workspace?
  • Line 5 -  What is getOutput?  Do I need to create a script tool from this code and add a parameter? If so, is getOutput a folder where I store the results? 

Thanks again

0 Kudos