我有一个 Flask 应用程序,其中包含创建 docx 文档然后将其转换为 pdf 的功能,当我在本地计算机上运行代码时一切正常并且创建了 pdf 文档但是当我在部署计算机上的 IIS 上托管应用程序时使用 fastcgi 模块,它工作正常并创建 docx 文档但由于错误而没有创建 pdf 文档,这里是创建 docx 文档并使用 docx2pdf 库转换为 pdf 的函数:
def CreateApplication(file,replacements,serialnum):
document=Document(file)
for para in document.paragraphs:
# Iterate through the runs in the paragraph
for run in para.runs:
# Check if the run text is a keyword in the replacements dictionary
if run.text in replacements:
# Replace the keyword with the replacement and maintain the style
run.text = replacements[run.text]
for table in document.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
for run in paragraph.runs:
for keyword, value in replacements.items():
if keyword in run.text:
# Replace the keyword with the value
run.text = run.text.replace(keyword, value)
document.save(f'files/{serialnum}/MainFiles/Application{serialnum}.docx')
docx_output_path=f'files/{serialnum}/MainFiles/Application{serialnum}.docx'
pdf_input_path = docx_output_path
# Specify the path of the output .pdf file
pdf_output_path = os.path.splitext(pdf_input_path)[0] + '.pdf'
# Convert the .docx file to .pdf
convert(pdf_input_path, pdf_output_path)
这是我在烧瓶中的称呼:
@app.route('/submitapplication/<serialnumber>',methods=['POST','GET'])
def submit(serialnumber):
st = time.time()
print('iam in')
datacoming=request.get_json()
print(datacoming)
project_describtion=json.loads(datacoming)
project_describtion['Statue_of_Project']='Application pending'
current_date = datetime.now()
formatted_date = current_date.strftime("%Y-%m-%d")
project_describtion['DateofApplication'] = formatted_date
print(project_describtion)
pidata = Search_in_Company(session['user'], session['password'], session['serverip'], session['companyid'])
project_describtion.update(pidata)
'''
write sql code to upload this dictionary to its specified rows
'''
project_describtion['Project_Serial_Number']=serialnumber
#CreateApplication('LPG application General.docx',project_describtion,serialnumber)
#trying threading to run conversion in another thread
Application=threading.Thread(target=CreateApplication,args=('LPG application General.docx',project_describtion,serialnumber))
Application.start()
# get the execution time
Update_case(session['user'],session['password'],session['serverip'],session['currentserial'],session['companyid'],project_describtion)
et = time.time()
elapsed_time = et - st
print('Execution time:', elapsed_time, 'seconds')
return jsonify({'url': url_for('home')})
这是我尝试 comtypes.client 库时出现的错误,当我使用 docx2pdf 库时出现类似的错误:
Exception on /submitapplication/JO-2023JL030039 [POST]
Traceback (most recent call last):
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\flpgproj\main.py", line 404, in submit
CreateApplication('LPG application General.docx',project_describtion,serialnumber)
File "C:\inetpub\wwwroot\flpgproj\CreateFiles.py", line 136, in CreateApplication
convert_to_pdf(pdf_input_path, pdf_output_path)
File "C:\inetpub\wwwroot\flpgproj\CreateFiles.py", line 35, in convert_to_pdf
word=comtypes.client.CreateObject('Word.Application')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\comtypes\client\__init__.py", line 215, in CreateObject
obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\comtypes\__init__.py", line 1264, in CoCreateInstance
_ole32.CoCreateInstance(byref(clsid), punkouter, clsctx, byref(iid), byref(p))
File "_ctypes/callproc.c", line 1000, in GetResult
OSError: [WinError -2146959355] Server execution failed
这里是我使用 docx2pdf 库时出现的完全相同的错误,请注意我将 word 和 pdf 的所有必需权限授予 defaultapplicationpool 和 iis_iusr:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 2525, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 1822, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 1820, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\flask\app.py", line 1796, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\inetpub\wwwroot\flpgproj\main.py", line 404, in submit
CreateApplication('LPG application General.docx',project_describtion,serialnumber)
File "C:\inetpub\wwwroot\flpgproj\CreateFiles.py", line 136, in CreateApplication
convert(pdf_input_path, pdf_output_path)
File "C:\Python\python311\Lib\site-packages\docx2pdf\__init__.py", line 106, in convert
return windows(paths, keep_active)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\docx2pdf\__init__.py", line 19, in windows
word = win32com.client.Dispatch("Word.Application")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\win32com\client\__init__.py", line 117, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\win32com\client\dynamic.py", line 106, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Python\python311\Lib\site-packages\win32com\client\dynamic.py", line 88, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)