Model Builder to Create Python Script for xls to csv Conversion

898
13
Jump to solution
02-12-2019 12:32 PM
MagdalenaMartinez
New Contributor

I am currently trying to create a python script within model builder to convert an xls file to csv.

I created one already using the "Excel to Table" tool which was successful. However, I tried to add data to the xls file and run the script again and it choked on the "csv already exists". I tried adding "Application.DisplayAlerts = False" to overwrite the csv already existing, but it still choked when I tried to run it again.

My ultimate goal is to create a script I can set up in task scheduler to run on it's own everyday to convert an xls to csv, without using a script that requires macros to be enabled in the xls.

I am at the beginner beginner level when it comes to python script. Any help would be much appreciated!

0 Kudos
13 Replies
StephenM
Occasional Contributor II

To overwrite outputs, set arcpy.env.overwriteOutput = True.

overwriteOutput
(Read and Write)

Controls whether tools will automatically overwrite any existing output when run. When set to True, tools will execute and overwrite the output dataset. When set to False, existing outputs will not be overwritten, and the tool will return an error.

Boolean

env—Help | ArcGIS Desktop

You could do this using Excel To Table to convert the Excel sheet to a table, then Table To Table to convert the table to a CSV.

In my testing the output CSV always had an OID field added to it, so I used the pandas library to drop the OID field.

import arcpy
import pandas as pd

arcpy.env.overwriteOutput = True

# Input Excel file
excel_file = r"C:\Users\Username\Desktop\test_book.xlsx"

# Intermediate converted table
interm_table = r"C:\Users\Username\Documents\ArcGIS\Default.gdb\test_book"

# Convert Excel file to table
arcpy.ExcelToTable_conversion(excel_file, interm_table, "Sheet1")

# Directory and name for the output CSV
output_dir = r"C:\Users\Username\Desktop"
csv_name = "test_book.csv"

# Convert intermediate table to CSV
arcpy.TableToTable_conversion(interm_table, output_dir, csv_name)

# Set full path of the CSV
csv_fullpath = output_dir + "\\" + csv_name

# Read CSV into pandas DataFrame
df = pd.read_csv(csv_fullpath)

# Drop OID column
df.drop('OID', axis=1, inplace=True)

# Write DataFrame to the same CSV as the input
df.to_csv(csv_fullpath, index=False)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

pandas.read_csv — pandas 0.24.1 documentation

pandas.DataFrame.drop — pandas 0.24.1 documentation

pandas.DataFrame.to_csv — pandas 0.24.1 documentation

View solution in original post

0 Kudos
curtvprice
MVP Esteemed Contributor

The OP asked about Model Builder. I think pandas is a little more than they were interested in... did you try simply modifying the field map on the Table to Table tool to drop the OID? Seems like that would be less trouble (especially if using Model Builder).

0 Kudos
StephenM
Occasional Contributor II

Yeah, I tried using FieldMappings and also tried using table view FieldInfo. With field mapping, the ObjectID field didn't seem to be accessible using the fields property of the FieldMappings object. With FieldInfo, even if I hid the ObjectID field it still showed up in the output CSV. Hiding fields worked with all the other fields, just not ObjectID.

MichaelBruening
New Contributor III

Just wanted to say thank you for posting this solution as I was able to use some of the code to help with a similar process of dropping the "OID" field from a CSV file that was exported from the "Table to Table" tool.

0 Kudos