How to convert a docx to pdf in flask

1.5k Views Asked by At

So i am making a flask web app and i added a docx to pdf converter but its not working every time i will ran into a problem even though it is simple so i am using this python package to convert docx to pdf https://pypi.org/project/docx2pdf/ and i made it till where i upload files in flask and it start converting. I get error when converting it

this is my code

@app.route("/pdf", methods=["POST"])
def pdf2docx_post():
    file = request.files['file']

    if file and allowed_file(file.filename):
        filename = file.filename
        file.save(secure_filename(filename))
        convert(filename, filename.split('.')[0] + '.docx')
        
        convert(filename, "output.pdf")
        convert(filename)
        return send_file("output.pdf", as_attachment=True)


        return render_template("pdf.html", wait="Pleas wait your download will start automatically")

i get this error

Traceback (most recent call last):
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\flask\app.py", line 2091, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\flask\app.py", line 2076, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\flask\app.py", line 2073, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\flask\app.py", line 1518, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\flask\app.py", line 1516, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\flask\app.py", line 1502, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\app\main.py", line 145, in pdf2docx_post
    convert(filename, filename.split('.')[0] + '.docx')
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\docx2pdf\__init__.py", line 102, in convert
    paths = resolve_paths(input_path, output_path)
  File "C:\Users\UwU\OneDrive\Desktop\UwU web\Web Development\all-in-one-tools\tools-web\Lib\site-packages\docx2pdf\__init__.py", line 94, in resolve_paths
    assert str(output_path).endswith(".pdf")
AssertionError

Thanks in advance

NOTE: I ALREADY USED MANY STACKOVERFLOW ANSWERS BUT IT DIDNT WORKED

2

There are 2 best solutions below

1
Advay168 On

From the error it seems the function expects the filename to end with ".pdf" so you could remove these two extra lines:

convert(filename, filename.split('.')[0] + '.docx')       
convert(filename)

and leave

convert(filename, "output.pdf")
0
Alankrith G On

You can do this..it will work.I struggled with this CoInitialize has not been called error too and it will work in your case too..

from docx2pdf import convert  
import os   
import pythoncom
convert(your_input_filepath, your_output_directory_path, pythoncom.CoInitialize())

For the your_input_filepath , your_output_directory_path you can give relative input path and output path using os.path.relpath("put_your_filepath_here")