0

I am trying to deploy a Flask application to Azure Web App Service. I am running Windows OS but Azure App Services only supports Python on Linux. I am getting errors whenever it loads the python modules that involve conversion of .docx to pdf. My container crashes and I get the following message:

:( Application Error
If you are the application administrator, you can access the diagnostic resources.

Below are the error messages I observe from the logs

I have tried docx2pdf and I got this error:

"/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'

I tried comtypes and got this error:

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' 

I am running on python 3.9.7. and for deployment I have tried the azure CLI on both Windows Powershell and VScode, and I've also tried deploying from Github but I'm still getting the same errors.

Is there anyway to solve the issues I have encountered? Or is there another way I can convert a docx file to pdf in linux using python?

Trelaquix
  • 23
  • 9

1 Answers1

0

To solve this AttributeError: 'PosixPath' object has no attribute 'read_text' error, You can refer to this GitHub issues: setup failed 'PosixPath' object has no attribute 'read_text' and 'PosixPath' object has no attribute 'read_text'

To solve this ImportError: cannot import name 'COMError' from '_ctypes' error, As per SuperBiasedMan COMTypes is designed for Windows, not Linux.

Thank you abdelhedi hlel. Posting your suggestion as an answer to help other community members.

You can try the below code to convert docx file to pdf on Linux Azure app services:

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)

You can refer to Converting DOCX to PDF using Python, Converting docx to pdf with pure python (on linux, without libreoffice) and How to convert Word document to PDF in Azure App service on Linux

DeepDave-MT
  • 1,861
  • 1
  • 5
  • 18