我们如何在 Django 中将上传的 docx 文件转换为 pdf

How can we convert uploaded docx file to pdf in django

我创建了一个接受文件的模型。我正在尝试将上传的 docx 文件转换为 pdf 并将其显示给用户。 为了将 docx 文件转换为 pdf,我正在尝试使用 python 的 docxtopdf 模块。 但是当我将我的文件从 request.FILES['Doc] 传递到转换函数时,它给出了错误:

TypeError at /convview/
expected str, bytes or os.PathLike object, not FieldFile

这里是 views.py 文件中的代码:

    def post(self, request, *args, **kwargs):
    form = self.get_form()
    if form.is_valid():
        pdffile=UserConvert(Doc=request.FILES['Doc'])
        pdffile.save()
        convert(pdffile.Doc)

当文件在内存中时,我们如何更改文件的 mime 类型?

您正在将 FieldFile 传递给函数,但需要字符串、字节或路径:

TypeError expected str, bytes or os.PathLike object, not FieldFile

您需要使用path参数获取文件的路径:

convert(pdffile.Doc.path)