tomcat/CGI和python3无法使用win32com打开Excel fi

2024-06-01 01:39:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个PY3脚本,它使用Win32COM实例化一个新的Excel实例,并尝试打开一个现有的.xlsx文件-在Windows上通过Tomcat/CGI调用时失败:

sys.stdout.write("Content-Type: text/html\n\n")
excel = DispatchEx('Excel.Application')

dir = r'C:\temp'
s = 'test.xlsx'
sfile = os.path.join(dir, s)

try:
    wbS = excel.Workbooks.Open(sfile)
except pythoncom.com_error as error:
    print("exception details: {0}".format(error))
    exit(1)

操作失败:

exception details: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', "Microsoft Excel cannot access the file 'C:\\temp\\test.xlsx'. There are several possible reasons:\n\n• The file name or path does not exist.\n• The file is being used by another program.\n• The workbook you are trying to save has the same name as a currently open workbook.", 'xlmain11.chm', 0, -2146827284), None)

当Tomcat以管理员或本地帐户的身份运行时失败,这两种帐户都具有对现有文件的完全控制访问权限。确保redispatch、Dispatch和DispatchEX不产生成功。没有其他应用程序正在访问/锁定该文件。在cmd中运行成功,表明在CGI级别丢失了一些东西。你知道吗

(使用COM允许在工作簿和XLSWriter不支持的其他活动之间复制工作表,等()


Tags: 文件thepath实例testdirerrorxlsx