python - 我们如何在 django 中将上传的 docx 文件转换为 pdf
问题描述
我创建了一个接受文件的模型。我正在尝试将上传的 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 类型?
解决方案
您正在将 a 传递FieldFile
给函数,但需要一个 str、字节或路径:
TypeError expected str, bytes or os.PathLike object, not FieldFile
您需要使用path
参数获取文件的路径:
convert(pdffile.Doc.path)
推荐阅读
- filter - 为什么我们在编译器中应用管道和过滤器而不是分层架构?
- php - 传递给 App\Models\Model::__construct() 必须是一个实例
- bash - 如何在 GMT PSTEXT 中使用符号字体?
- c# - 有没有办法只用 C# 构建一个 DataTemplate
- javascript - React Native - 使用正文打开邮件客户端
- javascript - 如何在动画速度期间禁用按钮?
- django - Django显示相关计数
- javascript - 在 JSONforms 中填充字段并创建动态表单
- javascript - 为 html 网页使用 nodejs 模块
- cuda - 尝试使用 FFT 卷积时不支持 cuDNN 状态