-1

I'm relatively new to programming, and I'm having some trouble with Python.

For reference, I've created a little python program that will load in a pdf, grab the relevant details, and upload it to my company's expense form. The program works well, but there's a problem: the code will only run on the pdf if I've hardcoded the location, like so:

receipt='C:\\Users\\\me\\Desktop\\expense app\\united receipt.pdf'

My goal is to make this an exe file that I can share with my co-workers, and have them right click on the pdf file and let the little macro run.

Here's the problem: I cannot figure out how to write the program so that the pdf is not hardcoded.

I already know how to make it into an exe file and put it into the context menu, but again, I don't know how to write it so that I can tell the program to pass the file I just selected.

Essentially, I want the code to say

receipt=file_you_just_clicked_on

Because it works just fine after the file is passed, but I don't know how to do that.

I've tried following the advice on this question: Run Python Script on Selected File

but either I'm completely misunderstanding what I'm supposed to be doing, or I've made the syntax all messed up.

Any help would be appreciated!

Please let me know if you need more context, this is my first time posting here.

1 Answers1

2

When a program is ran because it is associated with the file type or an action on the file type, it receives in sys.argv, the path to the file. This is also where it gets the name of a file that is dropped on it.

Forgive me I don't write programs on Windows and it has been decades since I used a Windows machine for anything serious. sys.argv normally starts with 0 being the script and then 1: being the arguments but on Windows the name of the Python executable might have been included resulting in the arguments starting at 2:

Please lookup how file associations work.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • When I run the program, sys.argv[1] returns as the python script itself, not the file that I'm sending to it. From what you said above, that's not what supposed to happen. What is going on? – Johnathon Kreider Sep 23 '19 at 01:58