Windows调度程序中未运行Python脚本的BAT文件
我正在尝试运行Python脚本以更新PPT演示文稿。一年前,我还尝试了此回归并在SQL中更新表,并且也没有运行。那时我放弃了,因为我无法解决。
我设法创建了一个BAT文件,以在Windows调度程序中运行R代码,并且有效。
我已经创建了BAT文件并在命令提示符中对其进行了测试,并且PY文件运行并更新了PPT演示文稿。
当我在Windows调度程序中运行此BAT文件时,不会更新PPT。
当前,BAT文件如下:
@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
这些是我到目前为止尝试的:
- 在BAT文件中添加了一个日志文件。创建日志文件并添加三个步骤,因此我知道BAT文件已运行。日志文件返回以下内容:
C:\python>call C:\ProgramData\Anaconda3\Scripts\activate.bat
(base) C:\python>cd C:\python\
(base) C:\python>python Updateppt.py
- 根据堆栈溢出的建议将BAT文件编辑为各种组合。他们中的大多数都在命令提示符中工作,但是在Windows调度程序中没有工作,
- 请检查我保存信息的文件夹上的安全设置,并且我拥有完整的访问,
- 请确保将文件夹添加到系统和用户段的环境变量中的pythonpath
- 具有当前通过Windows调度程序运行的R文件,因此确保属性中的所有一般,条件和设置部分都匹配一个
- 重新运行的PIP在所有软件包上安装,以确保它们可访问并在右边PY文件运行时的位置。这是基于此建议: 无法安排python脚本调度程序
- 定时了命令提示符和Windows调度程序任务,并且命令提示符需要30秒,而Windows Scheduler则需要20秒
- 添加到Python文件中,并且在启动脚本时会登录脚本,并且在Windows Scheduleler中运行时会记录一段时间因此,它正在运行Python脚本,
我该怎么做才能使此工作?我真的对此感到不知所措,似乎找不到堆栈溢出响应,即实际解决了我患有
更新
我在运行每个函数后添加了时间,就在上次之前添加了时间函数,日志文件显示在Windows调度程序中运行时,它不会运行最后一个函数,而是循环回到第一个函数。 中不执行此操作
它在命令提示符 Windows调度程序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
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
。运行“运行脚本停止”行。为什么它会在命令提示符中正确运行,而不是Windows调度程序?
这是它不运行的代码
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是该文件无法保存在网络驱动器中。该错误说它找不到文件路径。我使用了此代码并解决了问题:
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: