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')
  • That can't be the Python code that successfully runs. `chmod +x EU_YES.py` is not valid Python. – AKX Apr 08 '21 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. – AKX Apr 08 '21 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... – Angelina Klein Apr 08 '21 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. – AKX Apr 08 '21 at 12:51
  • Do you get any error message? If that's the case, it'd be helpful if you share it with us. – shaedrich Apr 08 '21 at 15:22
  • I don't get any error massages... – Angelina Klein Apr 09 '21 at 05:14
  • Finally i have it. Addet the whole python path instead of "only" python ... . Now it works! echo shell_exec("C:/Users/"myUser"/AppData/Local/Programs/Python/Python39/python.exe EU_YES.py"); – Angelina Klein Apr 09 '21 at 05:27

2 Answers2

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Alone Bashan
  • 116
  • 1
  • 7
  • 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... – Angelina Klein Apr 08 '21 at 12:00
  • sounds like you need to debug it well.
    are you starting the python file manually from your server?
    are you checking it on localhost? what is the output from the `exec`?
    if you are running your php with Xampp take a look at [https://stackoverflow.com/questions/42704846/running-python-scripts-with-xampp](https://stackoverflow.com/questions/42704846/running-python-scripts-with-xampp)
    – Alone Bashan Apr 08 '21 at 12:16
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')
AKX
  • 152,115
  • 15
  • 115
  • 172
  • 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... – Angelina Klein Apr 08 '21 at 13:06
  • So what happens if you access that PHP file in the browser? – AKX Apr 08 '21 at 13:10
  • Nothing is output as expected, but also no change in the Excel file ... – Angelina Klein Apr 08 '21 at 13:11
  • 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.) – AKX Apr 08 '21 at 13:12
  • You may also wish to add `error_reporting(E_ALL);` to your PHP file, and check the XAMPP error logs too. – AKX Apr 08 '21 at 13:13
  • and no "Hello!" – Angelina Klein Apr 08 '21 at 13:19