4

I have three python scripts. One gathers data from database(data_for_report.py), another generates report from that data and creaters .xlsx file(report_gen.py) and the last one modifies the style of that excel file(excel_style.py).

Now all three files are in the same directory and what I do now is simply execute scripts one after another in the interpreter to get the report. I want to make everything work with one click so people who need this report could do it themselves. I thought of creating an exe with pyinstaller, but I can not think of a way to link my scripts together so that when data_for_report.py ends its job report_gen.py is started and so on.

I tried to put

subprocess.call("report_gen.py", shell=True)

at the end of the first script, but nothing happens, I just get this:

Out[2]: 1

How could I do this?

milka1117
  • 521
  • 4
  • 8
  • 17

2 Answers2

5

Actually, This problem can be solved by using batch programming. Your python files will run in batches i.e. one file after the other. I am assuming your all three python files resides in folder ReportGenerator with Path as C:\ReportGenerator so adjust accordingly the PATH as of your system (Please care for \ and / in PATH of folder having the python files).

Your files are which need to be executed:

data_for_report.py
report_gen.py
excel_style.py

Now open a Notepad file and write the below lines.

cd C:/ReportGenerator
python data_for_report.py
python report_gen.py
python excel_style.py
PAUSE

Now save this file with file_Name.bat anywhere u want in system and remember it. After saving the batch file icon will form on saving. Now Open window command prompt and just drag this batch file to window command prompt.

Rink16
  • 138
  • 12
  • This approach won't work for Linux! How do you know the OP is running on Windows! – Devesh Kumar Singh Apr 26 '19 at 06:33
  • Actually, Batch files can be made in linux too for executing the python files, I will paste the link for that also. – Rink16 Apr 26 '19 at 06:38
  • Sure, but making a new python script to run all the scripts at one is more pythonic then writing a batch file in my opinion! – Devesh Kumar Singh Apr 26 '19 at 06:39
  • Actually, all three python files are independent and there main purpose is to generate the report i.e. Excel file, so while writing the batch file it won't affect much now you can make a single GUI button to just run the batch file. and That's all problem is solved with one click solution. – Rink16 Apr 26 '19 at 06:47
  • Sure I can also place the combined python file into the batch file and do the same! – Devesh Kumar Singh Apr 26 '19 at 06:54
  • 1
    what does the PAUSE do here? can I remove it? – uniquegino Dec 29 '20 at 23:56
4

Why not encapsulate all the logic for each script in a function, make a new file which imports all the 3 functions, and then run that script.

So if the scripts are

data_for_report.py

def f1():
  ...

report_gen.py

def f2():
  ...

excel_style.py

def f3():
  ...

Then the final script which you will run is :

from data_for_report import f1
from report_gen import f2
from excel_style import f3

f1()
f2()
f3()
Devesh Kumar Singh
  • 20,259
  • 5
  • 21
  • 40