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!
Solved! Go to Solution.
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 |
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
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).
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.
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.