-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.

2
  • Are you able to ask your coworkers to place the pdf file into the same folder where script is? Then it would be simple with os.path module. Commented Sep 22, 2019 at 21:43
  • @Nf4r probably not, they wouldn't do that. Any other ideas? Commented Sep 23, 2019 at 1:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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?

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.