如何使用 python 函数(WINDOWS)从 docx 转换为 pdf?

问题描述 投票:0回答:0

我正在使用带有 python 函数的 env 将 docx 转换为 pdf 文件。我正在使用邮递员发送 base64。然后我挂载 docx 文件(一切正常),但是当它将 docx 文件转换为 pdf 时,出现错误。我在想那是因为我的环境中没有 Office?我如何在没有办公室的情况下修复它?谢谢。

import sys
import os
import comtypes.client
import pythoncom
import uuid
import requests
from docx import Document
import base64
from os import listdir
from os.path import isfile, join
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
  bytesDoc = req.get_json()['base']

  path = '/users/echornet/pruebas/'
  newFile = open(path + 'prueba.docx','wb')
  newFile.write(base64.b64decode(bytesDoc))

  newFile.close()
  wdFormatPDF = 17

  out_file = path + 'prueba.pdf'
  word = comtypes.client.CreateObject('Word.Application')

  doc = word.Documents.Open(newFile)
  doc.SaveAs(out_file, FileFormat=wdFormatPDF)
  doc.Close()

这是我遇到的错误。我得到了从 base64 创建的 docx,但没有转换。

System.Private.CoreLib: Exception while executing function: 函数.FunConverter。 System.Private.CoreLib:结果:失败 异常:AttributeError:模块“comtypes.gen.Word”没有属性 '_Application' 堆栈:文件 "C:\PruebaFunction\ConvEnv\lib\site-packages zure unctions_worker\dispatcher.py", 第 288 行,在 _handle__invocation_request self.run_sync_func, invocation_id, fi.func, args) 文件 "C:\Users chornet\AppData\Local\Programs\Python\Python36\li

python ms-word converters comtypes
© www.soinside.com 2019 - 2024. All rights reserved.