Search code examples
pythondjangodjango-modelsfile-conversionfilefield

How can we convert uploaded docx file to pdf in django


I have created a model which accepts file. I'm trying to convert the uploaded docx file to pdf and display it to the user. for the conversion of docx file to pdf I'm trying to use docxtopdf module of python. But when I pass my file from request.FILES['Doc] to convert function it gives error :

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

Here i the code from views.py file:

    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)

How can we change mime type of files when it is in memory?


Solution

  • You are passing a FieldFile to the function but a str, bytes or a path is expected:

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

    You need to get the path of the file with the path parameters:

    convert(pdffile.Doc.path)