word.SaveAs() - 'NoneType' 对象没有属性 'SaveAs' - 使用任务计划程序运行时

问题描述 投票:0回答:1

我遇到了一个奇怪的问题,我对可能发生的事情感到非常迷失。一般背景是我循环遍历一个文件夹并将所有“.docx”文件转换为“.pdf”文件。令人困惑的部分是,当我进入

for
循环时,通过 Windows 任务计划程序触发而无需显式重写文件路径,它不再正常工作。我以我的用户(管理员权限)身份使用 Windows 任务计划程序,并具有登录或注销状态,具有最高权限。这与我手动触发时使用的用户相同。这是在 RDP 服务器上。

以下是更多详细信息:

脚本 1:使用任务计划程序触发时起作用

from win32com import client
import os
import sys

#import Custom Classes
Classes = r'C:\Python_Solutions\Classes'

#Direct import to Class file
sys.path.insert(0,Classes)

import Classes

#Declare jobName
jobName = 'pdfsave'
logFolderLocation = r'C:\Python_Solutions\Notice_Form_Generation\pdftest'

#Create Log Folder
Classes.Atlas.createTextJobLog(jobName, logFolderLocation)

try:
    print('before')
    for file in os.listdir(r'C:\Python_Solutions\Notice_Form_Generation\Local\2022-01-01\1_FixedPrem1stAttempt'):
        print(str(file))
        if '.docx' in file:
            filepath = r'C:\Python_Solutions\Notice_Form_Generation\Local\2022-01-01\1_FixedPrem1stAttempt\\' + file
            print('Converting to .pdf: ' + filepath)
            endFilePath_pdf = filepath[:-4] + 'pdf'
            print('here1')
            word = client.DispatchEx("Word.Application")
            print('here2')
            worddoc = word.Documents.Open(filepath, ReadOnly=1)
            print('here3')
            worddoc.SaveAs(endFilePath_pdf, FileFormat=17)
            print('here4')
            worddoc.Close()
            print('here5')
            word.Quit()
            print('here6')
except Exception as e:
    print(str(e))

脚本 2:当我自己触发或运行脚本时可以工作,但当我使用任务计划程序运行时会失败并出现错误

'NoneType' object has no attribute 'SaveAs'
。我很困惑这是怎么回事,正如您在代码中看到的,我正在仔细检查文件路径以确保它们正确地输入到函数中,但它不想工作..

here3
功能期间,在
.SaveAs()
之后它会中断:

from win32com import client
import os
import sys

#import Custom Classes
Classes = r'C:\Python_Solutions\Classes'

#Direct import to Class file
sys.path.insert(0,Classes)

import Classes


#Declare jobName
jobName = 'pdfsave_simple2'
logFolderLocation = r'C:\Python_Solutions\Notice_Form_Generation\pdftest'


# In[ ]:


#Create Log Folder
Classes.Atlas.createTextJobLog(jobName, logFolderLocation)

folder = r'C:\Python_Solutions\Notice_Form_Generation\Local\2022-01-01\1_FixedPrem1stAttempt'

try:
    print('before')
    for file in os.listdir(folder):
        print(str(file))
        if '.docx' in file:
            filepath = folder + '\\' + file
            print('Converting to .pdf: ' + filepath)
            endFilePath_pdf = filepath[:-4] + 'pdf'
            print('here1')
            word = client.DispatchEx("Word.Application")
            print('here2')

            print(os.path.abspath(filepath))
            print(os.path.abspath(endFilePath_pdf))

            worddoc = word.Documents.Open(filepath, ReadOnly=1)
            print('here3')
            worddoc.SaveAs(endFilePath_pdf, FileFormat=17)  # <-- here3
            print('here4')
            worddoc.Close()
            print('here5')
            word.Quit()
            print('here6')
except Exception as e:
    print(str(e))
    sys.stdout.close()
    sys.exit()

我在脚本 2 中尝试过的事情:

--- 将abs路径添加到函数中(我得到同样的错误):

worddoc = word.Documents.Open(os.path.abspath(filepath), ReadOnly=1)
worddoc.SaveAs(os.path.abspath(endFilePath_pdf), FileFormat=17)

--- 在发送到单词函数之前尝试使用

r'example\example
重新创建文件路径(当我尝试手动运行时,这实际上会出现相同的错误):

filepath = "r'" + os.path.abspath(filepath) + "'"
endFilePath_pdf = "r'" + os.path.abspath(endFilePath_pdf) + "'"

--- 当我检查目录时,手动运行和使用调度程序运行时它们都是相同的:

pathlib.Path(__file__).parent.resolve()

我相信这是错误的,任何人都可以帮忙(详细信息如下)

当我在本地尝试以下操作时,我得到:

            print('cwd: ' + str(os.path.abspath(os.getcwd())))
            print('script run: ' + str(pathlib.Path(__file__).parent.resolve()))
  • cwd:
    C:\Python_Solutions\Notice_Form_Generation\pdftest
  • 脚本运行:
    C:\Python_Solutions\Notice_Form_Generation\pdftest

当我使用任务计划程序运行时,我得到

  • cwd:
    C:\Windows\system32
  • 脚本运行:
    C:\Python_Solutions\Notice_Form_Generation\pdftest

我用以下内容更改了 cwd,但仍然得到相同的

NoneType
错误:

abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)

我有什么遗漏的吗?对我来说很奇怪的是,我在这两种情况下都在函数中输入了完全相同的字符串,甚至在手动触发时它也能工作,但是当通过任务调度程序触发时,我收到

NoneType
错误!我不知道会发生什么,当使用任务计划程序运行时,我是否必须以某种方式更具体地编写路径?或者也许在进入之前确保我处于正确的cwd?我很惊讶,因为我发现当我在for循环中写入文件路径时它可以工作,但是我需要迭代多个文件夹,所以我需要能够在
for
循环中不显式写入文件夹路径。我正在使用循环来遍历许多文件夹和文件(在此脚本的较大规模情况下)。

更新 - 我尝试使用 comtypes.client,但在步骤 3 之前失败并出现 NULL COM 指针访问

            print('1')
            word = comtypes.client.CreateObject('Word.Application')
            print('2')
            doc = word.Documents.Open(filepath)
            print('3')
            doc.SaveAs(endFilePath_pdf, FileFormat=17)
            print('4')
            doc.Close()
            print('1')
            word.Quit()
    ```
python docx win32com python-docx windows-task-scheduler
1个回答
0
投票

我知道这是一篇旧帖子,但在 Windows Server 2022 和 Windows 10 Pro 上以注销用户身份使用计划任务运行 python 将 docx 保存为 PDF 时,我遇到了相同的 NoneType 错误。

奇怪的是,它在 Windows 11 上按预期工作。

不幸的是,我还没有弄清楚为什么。感谢OP确认我没有失去理智。如果找到解决方案,我会更新。

© www.soinside.com 2019 - 2024. All rights reserved.