1

I have little to no background in Python or computer science so I’ll try my best to explain what I want to accomplish. I have a Pandas script in Jupyter notebook that edits an Excel .csv file and exports it as an Excel .xlsx file. Basically the reason why we want to do this is because we get these same Excel spreadsheets full of unwanted and disorganized data from the same source. I want other people at my office that don’t have Python to be able to use this script to edit these spreadsheets. From what I understand, this involves creating a standalone file.

Here is my code from Pandas that exports a new spreadsheet:

import pandas as pd
from pandas import ExcelWriter    

test = pd.DataFrame.from_csv('J:/SDGE/test.csv', index_col=None)

t = test
    for col in ['Bill Date']:
t[col] = t[col].ffill()
T = t[t.Meter.notnull()]
T = T.reset_index(drop=True)
writer = ExcelWriter('PythonExport.xlsx')
T.to_excel(writer,'Sheet5')
writer.save()

How can I make this code into a standalone executable file? I've seen other forums with responses to similar problems, but I still don't understand how to do this.

leck95
  • 13
  • 3
  • Basically you need to bundle your Python script up as an executable. though this isn't recommend and you are sure to run into some dist specific problems. download cxfreeze on yoursystem, Then follow [This](http://cx-freeze.readthedocs.io/en/latest/script.html#script) guide. – Srini Aug 01 '17 at 17:54
  • Do you mean you just want a python `.py` file to call? – TayTay Aug 01 '17 at 17:59

2 Answers2

1

First, you need to change some parts in your code to make it work for anybody, without the need for them to edit the Python code. Secondly, you will need to convert your file to an executable (.exe).

There is only one part in your code that needs to be changed to work for everyone: the csv file name and directory

Since your code only works when the file "test.csv" is in the "J:/SDGE/" directory, you can follow one of the following solutions:

  • Tell everyone who uses the program that the file must be in a precise public directory and named "test.csv" in order to work. (bad)
  • Change your program to allow for input from the user. This is a little more complex, but is the solution that people probably want:

Add an import for a file selector at the top:

from tkinter.filedialog import askopenfilename

Replace

'J:/SDGE/test.csv'

With

askopenfilename()

This should be the final python script:

import pandas as pd
from pandas import ExcelWriter
from tkinter.filedialog import askopenfilename #added this    

test = pd.DataFrame.from_csv(askopenfilename(), index_col=None)

t = test
    for col in ['Bill Date']:
t[col] = t[col].ffill()
T = t[t.Meter.notnull()]
T = T.reset_index(drop=True)
writer = ExcelWriter('PythonExport.xlsx')
T.to_excel(writer,'Sheet5')
writer.save()

However, you want this as an executable program, that way others don't have to have python installed and know how to run the script. There are several ways to turn your new .py file into an executable. I would look into this thread.

user3750325
  • 1,502
  • 1
  • 18
  • 37
0

If you want to run a python script on anyone's system, you will need to have Python installed in that system. Once you have that, just create a .bat file for the command that you'd be using to execute the python file through CMD.

Step 1: Open Notepad and create a new file

Step 2: Write the command as follows in the file (Just replace the path and filename according to you)

python file.py

Step 3: Save it as script.bat (Select All Types from the list of file types while saving)

Now you can run that batch file as any other program and it will run the code for you. The only thing you need to make while you distribute this batch file and python script is to make sure that both the files are kept in the same location. Or else you will have to add the full path in front of file.py

asanoop24
  • 449
  • 4
  • 13