我正在尝试将Flask应用程序部署到Azure Web应用程序服务。我正在运行Windows OS,但Azure应用服务仅支持Linux上的Python。每当加载涉及.docx到pdf转换的python模块时,我都会收到错误。我的容器崩溃了,我得到了以下消息:
:( Application Error
If you are the application administrator, you can access the diagnostic resources.
以下是我从日志中观察到的错误消息
我尝试过docx2pdf,得到了这个错误:
"/opt/python/3.9.0/lib/python3.9/importlib/metadata.py", line 511, in read_text
return self._path.joinpath(filename).read_text(encoding='utf-8')
AttributeError: 'PosixPath' object has no attribute 'read_text'
我尝试了comtype,得到了这个错误:
File "/tmp/8d97989fbb07031/antenv/lib/python3.9/site-packages/comtypes/__init__.py", line 23, in <module>
from _ctypes import COMError
ImportError: cannot import name 'COMError' from '_ctypes'
我在python 3.9.7上运行。对于部署,我已经在Windows Powershell和VScode上尝试了azure CLI,我也尝试了从Github部署,但我仍然收到相同的错误。
有什么办法可以解决我遇到的问题吗?或者有没有其他方法可以在linux中使用python将docx文件转换为pdf?
发布于 2021-09-20 09:49:13
要解决此AttributeError: 'PosixPath' object has no attribute 'read_text'
错误,您可以参考以下GitHub问题:setup failed 'PosixPath' object has no attribute 'read_text'和'PosixPath' object has no attribute 'read_text'
为了解决这个ImportError: cannot import name 'COMError' from '_ctypes'
错误,按照SuperBiasedMan的说法,COMTypes是为Windows而不是Linux设计的。
谢谢你abdelhedi hlel。将您的建议作为答案发布,以帮助其他社区成员。
您可以尝试以下代码在Linux Azure应用程序服务上将docx文件转换为pdf
import sys
import subprocess
import re
def convert_to(folder, source, timeout=None):
args = [libreoffice_exec(), '--headless', '--convert-to', 'pdf', '--outdir', folder, source]
process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
filename = re.search('-> (.*?) using filter', process.stdout.decode())
return filename.group(1)
def libreoffice_exec():
# TODO: Provide support for more platforms
if sys.platform == 'darwin':
return '/Applications/LibreOffice.app/Contents/MacOS/soffice'
return 'libreoffice'
result = convert_to('TEMP Directory', 'Your File', timeout=15)
你可以参考Converting DOCX to PDF using Python,Converting docx to pdf with pure python (on linux, without libreoffice)和How to convert Word document to PDF in Azure App service on Linux
https://stackoverflow.com/questions/69218177
复制相似问题