【发布时间】:2022-12-01 11:13:38
【问题描述】:
我正在开发 Django (v 3.2.6) 应用程序 (Python 3.9.1.),它需要使用 pywin32.com 写入 Excel 文件。
在客户端,它工作正常,但是当我在 Windows 11 服务器上使用 IIS (v 10) 投入生产时,出现上述错误。
我有一个读取用户输入的文件并写入项目目录的例程:
if request.method == 'POST':
# Create a form instance and populate it with the file from the request (binding):
form = Name1_uploadForm(request.POST, request.FILES)
if form.is_valid():
# Create variable for uploaded file
uploaded_excel_file = form.cleaned_data['excel_file']
# Write it to BASE_DIR
with open(os.path.join(settings.BASE_DIR, form.cleaned_data['excel_file'].name), 'wb+') as destination:
for chunk in uploaded_excel_file.chunks():
destination.write(chunk)
# Allow the write process to conclude
time.sleep(12)
# Close the file
destination.close()
# Call conversion function
Name1_extraction(os.path.join(settings.BASE_DIR, form.cleaned_data['excel_file'].name))
# redirect to a new URL:
return HttpResponseRedirect(reverse('index') )
else:
form = Name1_uploadForm()
这会调用另一个函数(如下)来打开同一个文件:
def Name1_extraction(uploaded_excel_file):
const = win32.constants
# Need to run CoInitialize to use win32com.client
pythoncom.CoInitialize()
# Open Name1 excel with Win32com
excelFile = win32.gencache.EnsureDispatch('Excel.Application')
完整的错误如下:
enter image description here enter image description here
执行以下代码行时会发生错误:
excelFile = win32.gencache.EnsureDispatch('Excel.Application')
应用程序池是 IIS AppPool\DefaultAppPool。
DefaultAppPool 已被授予对文件夹 C:\Windows\SysWOW64\config\systemprofile\Desktop 和 C:\Windows\System32\config\systemprofile\Desktop 的完全访问权限
通过这些操作,我不希望看到任何错误
感谢您提供的任何帮助。
【问题讨论】:
-
不支持服务器端 Office 自动化(IIS 上 Web 应用程序中的 Office COM 互操作),所以不要浪费任何人的时间。
标签: python-3.x django excel iis pywin32