I am trying run a python script to update a ppt presentation. I have also tried this a year ago with running a regression and updating a table in SQL and didn't run either. I gave up then as I couldn't resolve it.
I have managed to create a bat file to run R code in windows scheduler and that works.
I have created the bat file and tested it in command prompt and it the py file runs and updates the ppt presentation.
When I run this bat file in windows scheduler is doesn't update the ppt.
Currently the bat file is as follows:
@echo off
SET log_file=C:\python\logfile.txt
echo on
call :logit >>log_file=%
exit /b 0
:logit
call C:\ProgramData\Anaconda3\Scripts\activate.bat
cd C:\python\
python Updateppt.py
These are the things I have tried so far:
- Added a log file to the bat file. The log file is created and adds the three steps so I know the bat file is run. The log file returns this:
C:\python>call C:\ProgramData\Anaconda3\Scripts\activate.bat
(base) C:\python>cd C:\python\
(base) C:\python>python Updateppt.py
- Edited the bat file to various combinations based on recommendations from stack overflow. Most of them have worked in command prompt but none work in windows scheduler
- Check the security settings on the folder where I am saving the information and I have full access
- Made sure the folder is added to the PYTHONPATH in both system and user sections for environment variables
- Have an R file that currently runs via a bat file through windows scheduler so have made sure all the general, conditions and settings sections in the properties match that one
- re-run pip install on all packages to make sure they are accessible and in the right location when the py file runs. This was based on this advice: Cannot schedule a python script to run through Windows Task Scheduler
- Timed the command prompt and windows scheduler tasks and the command prompts takes 30 seconds whereas the windows scheduler takes 20 seconds
- Added logging into python file and it logs when script is started and it logs a time when in running in windows scheduler so it is running the python script
Is there anything I can do to get this working? I am really at a loss with this and I can't seem to find a stack overflow response that actual solves the issue I am having
UPDATE
I have added times after each function is run and right before the last function, the log file shows that when it is run in windows scheduler, it doesn't run the last function but instead loops back to the first one. It doesn't do this in command prompt
windows scheduler run log of python
INFO:root:run script started at 2022-04-29 13:18:31.318567
INFO:root:loaded enc data at 2022-04-29 13:18:32.072627
INFO:root:create enc_id at 2022-04-29 13:18:32.075627
INFO:root:agg data at 2022-04-29 13:18:59.782707
INFO:root:run script started at 2022-04-29 13:19:22.904437
INFO:root:loaded enc data at 2022-04-29 13:19:23.225462
INFO:root:create enc_id at 2022-04-29 13:19:23.228464
command prompt log of python
INFO:root:run script started at 2022-04-29 13:20:48.871881
INFO:root:loaded enc data at 2022-04-29 13:20:49.051893
INFO:root:create enc_id at 2022-04-29 13:20:49.054894
INFO:root:agg data at 2022-04-29 13:21:05.040096
INFO:root:run script stopped at 2022-04-29 13:21:05.436125
It should aggregate the data and then export to ppt and the script will stop and run the 'run script stopped' line. Why would it be running it correctly in command prompt but not windows scheduler?
This is the code it's not running
def update_ppt(CHW_daily):
daily_figures = Presentation(ResultPath 'Template/daily_figures_template.pptx')
# CHW table
slide_CHW = daily_figures.slides[0]
table_CHW = [shape for shape in slide_CHW.shapes if shape.has_table]
#Then we can update the values in each cell directly from the dataframe:
for i in range(1,8):
for j in range(0,6):
table_CHW[0].table.cell(i,j).text = str(CHW_daily.iloc[i-1, j])
table_CHW[0].table.cell(i,j).text_frame.paragraphs[0].font.size = Pt(14)
daily_figures.save(ResultPath 'daily_figures.pptx')
return()
CodePudding user response:
The issue was the file wasn't able to save in the Network drive. The error said it couldn't find the file path. I used this code and solved the issue:
from pathlib import Path
ResultPath = Path(r'<network domain name>/Daily figures/')
ResultPath_str = str(ResultPath)
daily_figures = Presentation(ResultPath_str '/' Template/daily_figures_template.pptx')