0

I've already written a Python script that writes something into an existing Excel list (openpyxl). I need to develop a web application, after a button has been clicked the right Python script must running. I use a server and program on a Windows computer. I would appreciate any help.

Running Python scripts with Xampp --> done it already.

I don't need any output on the website, I just want the .py file to be executed, i.e. written to my Excel file.

This is my .php Code:

<?php
   exec('python C:/xampp/htdocs/QMPlan/EU_YES.py');
?>

This is my .py Code:

#!C:/Users/'myUser'/AppData/Local/Programs/Python/Python39/python.exe
import openpyxl

book = openpyxl.load_workbook('ISSDDE.xls')

sheet = book.get_sheet_by_name("General Description")

sheet['B9'] = "Yes"

book.save('ISSDDE.xls')
7
  • That can't be the Python code that successfully runs. chmod +x EU_YES.py is not valid Python. Commented Apr 8, 2021 at 12:44
  • Considering your script relies on relative paths, you will need to make sure you have the correct working directory when you execute the program. There's no guarantee what the working directory is when you run it with PHP like that. Commented Apr 8, 2021 at 12:45
  • Ok, then where i must put the "chmod +x EU_YES.py"? Okay, but it is the right complete path... i don' know what to do... Commented Apr 8, 2021 at 12:49
  • You don't need to chmod anything since you're not attempting to run things as executables. The "Run Python with Xampp" instructions you have are a red herring here, as you're okay running the Python script via PHP. Commented Apr 8, 2021 at 12:51
  • Do you get any error message? If that's the case, it'd be helpful if you share it with us. Commented Apr 8, 2021 at 15:22

2 Answers 2

0

maybe
exec('python <path-to-your-python-file>')
but that means that the PHP is running on the same machine as the python file. also, you can't run it directly from your browser, you'll have to run this code from your server.

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

2 Comments

I've already tried that with "exec ..". Yes, I start the code from my server. When I open my .php page, nothing happens in my Excel list. If I start my Python file individually, an expected change is made...
sounds like you need to debug it well. <br> are you starting the python file manually from your server?<br> are you checking it on localhost? what is the output from the exec?<br> if you are running your php with Xampp take a look at https://stackoverflow.com/questions/42704846/running-python-scripts-with-xampp
0

This will assume that you can successfully run python on your machine's command line too (i.e. that it is in PATH). If it's not, you will need to change that python to the full path to your Python executable, e.g. C:/Users/User/AppData/Local/Programs/Python/Python39/python.exe.

<?php
// Change directory to where the script is, so any relative paths are correctly resolved
chdir("C:/xampp/htdocs/QMPlan");
// Run the script and print the output, if any.
echo shell_exec("python EU_YES.py");
?>

As for the Python script, you don't need the #! shebang line or the chmod (which indeed is a syntax error). A plain script should do.

import openpyxl

book = openpyxl.load_workbook('ISSDDE.xls')

sheet = book.get_sheet_by_name("General Description")

sheet['B9'] = "Yes"

book.save('ISSDDE.xls')

6 Comments

Unfortunately I still have no improvement ... So my Python "application" is in "C: /Users/'myUser'/AppData/Local/Programs/Python/Python39/python.exe" and my py. File in "C: /xampp/htdocs/QMPlan/EU_YES.py". Do I have to change something in chdir () or not? Because i don't understand what "chdir" does...
So what happens if you access that PHP file in the browser?
Nothing is output as expected, but also no change in the Excel file ...
If you add print("Hello!") to the Python file, that should get output. (This is to test that the Python script gets executed at all.)
You may also wish to add error_reporting(E_ALL); to your PHP file, and check the XAMPP error logs too.
|

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.