首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Linux (Azure App Services)上运行python word (.docx)到pdf的转换器会出现问题吗?

在Linux (Azure App Services)上运行python word (.docx)到pdf的转换器会出现问题吗?
EN

Stack Overflow用户
提问于 2021-09-17 05:30:40
回答 1查看 204关注 0票数 0

我正在尝试将Flask应用程序部署到Azure Web应用程序服务。我正在运行Windows OS,但Azure应用服务仅支持Linux上的Python。每当加载涉及.docx到pdf转换的python模块时,我都会收到错误。我的容器崩溃了,我得到了以下消息:

代码语言:javascript
运行
复制
:( Application Error
If you are the application administrator, you can access the diagnostic resources.

以下是我从日志中观察到的错误消息

我尝试过docx2pdf,得到了这个错误:

代码语言:javascript
运行
复制
"/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,得到了这个错误:

代码语言:javascript
运行
复制
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?

EN

回答 1

Stack Overflow用户

发布于 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

代码语言:javascript
运行
复制
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'
代码语言:javascript
运行
复制
result = convert_to('TEMP Directory',  'Your File', timeout=15)

你可以参考Converting DOCX to PDF using PythonConverting docx to pdf with pure python (on linux, without libreoffice)How to convert Word document to PDF in Azure App service on Linux

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69218177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档