Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python函数(WINDOWS)将docx转换为pdf?_Python_Ms Word_Converters_Comtypes - Fatal编程技术网

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

如何使用python函数(WINDOWS)将docx转换为pdf?,python,ms-word,converters,comtypes,Python,Ms Word,Converters,Comtypes,我正在使用一个带有python函数的env将docx转换为pdf文件。我在用邮递员寄base64。然后我挂载docx文件(一切都正常),但是当它将docx文件转换为pdf时,会出现一个错误。我想那是因为我的环境里没有办公室?没有办公室我怎么能修好它?谢谢 import sys import os import comtypes.client import pythoncom import uuid import requests from docx import Document import

我正在使用一个带有python函数的env将docx转换为pdf文件。我在用邮递员寄base64。然后我挂载docx文件(一切都正常),但是当它将docx文件转换为pdf时,会出现一个错误。我想那是因为我的环境里没有办公室?没有办公室我怎么能修好它?谢谢

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:执行函数时发生异常: Functions.FunConverter。System.Private.CoreLib:结果:失败 异常:AttributeError:模块“comtypes.gen.Word”没有属性 “\u应用程序”堆栈:文件 “C:\PruebaFunction\ConvEnv\lib\site packages\azure\functions\u worker\dispatcher.py”, 第288行,处理调用请求中 self.运行\u sync\u func、调用\u id、fi.func、args)文件“C:\Users\echornet\AppData\Local\Programs\Python\Python36\lib\concurrent\futures\thread.py”, 第55行,运行中 结果=self.fn(*self.args,**self.kwargs)文件“C:\PruebaFunction\ConvEnv\lib\site packages\azure\functions\u worker\dispatcher.py”, 第347行,在运行同步功能中 返回func(**params)文件“C:\PruebaFunction\FunConverter\uuuu init.py”,主菜单第32行 word=comtypes.client.CreateObject('word.Application')文件“C:\PruebaFunction\ConvEnv\lib\site packages\comtypes\client\uuuuuu init\uuuuuuu.py”, 第250行,在CreateObject中 返回_manage(obj,clsid,interface=interface)文件“C:\PruebaFunction\ConvEnv\lib\site packages\comtypes\client\uuuuuu init\uuuuuu.py”, 第188行,in_manage obj=GetBestInterface(obj)文件“C:\PruebaFunction\ConvEnv\lib\site packages\comtypes\client\uuuuuu init\uuuuuuuu.py”, 第112行,在GetBestInterface中 接口=getattr(mod,itf_名称)


你可以试试lib win32com来完成这个任务

# -*- encoding: utf-8 -*-
import  os
from win32com import client
#pip instatll win32com
def doc2pdf(doc_name, pdf_name):
    """
    :word to pdf
    :param doc_name word file name
    :param pdf_name to_pdf file name
    """
    try:
        word = client.DispatchEx("Word.Application")
        if os.path.exists(pdf_name):
            os.remove(pdf_name)
        worddoc = word.Documents.Open(doc_name,ReadOnly = 1)
        worddoc.SaveAs(pdf_name, FileFormat = 17)
        worddoc.Close()
        return pdf_name
    except:
        return 1
if __name__=='__main__':
    doc_name = "f:/test.doc"
    ftp_name = "f:/test.pdf"
    doc2pdf(doc_name, ftp_name)

您可以使用python库
docx2pdf
,该库内部使用
win32com

安装:

pip install docx2pdf
用法:

from docx2pdf import convert
convert("input.docx", "output.pdf")
正如您所提到的,这种方法确实需要安装Microsoft Office


免责声明:我编写了此库和命令行工具。

我在Windows上运行的可能重复我在第行中遇到错误:“word=client.DispatchEx(“word.Application”)”TypeError:无法对传出的TypedData进行编码:如果在循环中运行,转换多个
.docx
文件,转换了几个文件后,它会被击中。@cph\u sto您是在Windows还是macOS上运行的?