0

I want to add this script to an ArcToolbox for a coworker, so that they can reorder a table's columns after exporting a table to excel. They are not savvy with code, so I'm trying to just provide them with a way to enter the file, and the path for the result.

I've tried to set the input as a file, and the output as a location, so that the tool will create a file at the output location using the 'Tool Properties' in ArcCataloge. right clicking the script in Catalog > Properties > navigate to Parameters tab

import pandas as pd
from pandas import DataFrame
from datetime import datetime

DY = datetime.now()
yr = DY.year
mt = DY.month

def reIndexContracts(inputFile, outFilePath):    
   df = pd.read_excel(inputFile)
   df = df.reindex(columns=['col1','col2', etc...])
   df.to_excel(r"{0}FileName{1}_{2}.xlsx".format(outFilePath,mt,yr))

if __name__ == "__main__":
    reIndexContracts(arcpy.GetParameter(0),
                     arcpy.GetParameter(1))

Unfortunately, I get an error back that the file location does not exist, and it cites the entire path and file with the extension in the error. For example:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\user\\Desktop\\the\\outpath\\FileName10_2022.xlsx"

What am I missing here? Hoping to just put a file and a path in, get a file out.

1 Answer 1

0

Usually, what I do if I want to create an output file would be to set the output location as a DEFolder (or folder) and as input. Then the filename as a GPString (or string).

The only thing in the code that might be an issue is:

r"{0}FileName{1}_{2}.xlsx".format(outFilePath,mt,yr)

Doesn't have a slash before the FileName. Also the r shouldn't be necessary.

"{0}\\FileName{1}_{2}.xlsx".format(outFilePath,mt,yr)

I assume the FileNotFoundError is happening on to_excel and not read_excel?

You might consider instead of just GetParameter use GetParameterAsText. You can also use arcpy.AddMessage(arcpy.GetParameterAsText(0)) to make sure you are getting the values you expect to receive.

2
  • Thanks! Ill try all of these and see what happens. Commented Oct 11, 2022 at 20:39
  • Yes the error is in the "to_excel" function Commented Oct 11, 2022 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.