使用Python检查是否使用任务计划程序打开程序时出现问题

时间:2019-05-30 18:07:19

标签: python taskscheduler

仅当我使用Windows Task Scheduler执行脚本时,我才检查Excel工作簿是否已经用Python打开时遇到了一些麻烦。 我有:

from win32com.client import dynamic

def test1():
    com_app = dynamic.Dispatch('Excel.Application')
    com_wbs = com_app.Workbooks
    wb_names = [wb.Name for wb in com_wbs]

仅当手动执行.pyw文件时,该方法才能正常工作,它返回包含打开的Excel工作簿名称的列表。但是,当通过Windows任务计划程序对其执行进行编程时,它只会返回一个空列表。

类似的事情也会发生:

import tkinter
from tkinter import messagebox
import win32com.client

def test2():
    try:
        excel = win32com.client.GetActiveObject("Excel.Application")
        messagebox.showinfo("Info", "An opened Excel file was found!")
    except:
        messagebox.showinfo("Info", "No opened Excel file!")

该函数在手动执行.pyw时会很好地检测到打开的文件,但在任务计划程序执行该操作时无法检测到。

有人可以帮我吗?非常感谢你!

0 个答案:

没有答案