Skip to content
Advertisement

DOCX to PDF converter program

I’m making a doc to pdf converter program, and i have occured an error as below: Error

Im an begginer in programming and i dont know how to fix it, below i paste python code:

from flask import Flask
from flask import request, render_template, redirect, url_for, send_file
import os
from typing import Tuple
from docx2pdf import convert
#from tkinter import Tk,messagebox
#from tkinter import _tkinter
UPLOADER_FOLDER = ''
app = Flask(__name__)
app.config['UPLOADER_FOLDER'] = UPLOADER_FOLDER

@app.route('/')
@app.route('/index', methods=['GET', 'POST'])
def index():
    if request.method == "POST":
        def docx2pdf(input_file: str, output_file: str, pages: Tuple = None):
           if pages:
               pages = [int(i) for i in list(pages) if i.isnumeric()]

           result = convert(docx_file=input_file, pdf_file=output_file, pages=pages)
           summary = {
               "File": input_file, "Pages": str(pages), "Output File": output_file
            }

           print("n".join("{}:{}".format(i, j) for i, j in summary.items()))
           return result
        file = request.files['filename']
        if file.filename!= '':
           file.save(os.path.join(app.config['UPLOADER_FOLDER'], file.filename))
           input_file = file.filename
           output_file = r"hello.pdf"
           docx2pdf(input_file, output_file)
           pdf = input_file.split(".")[0]+".pdf"
           print(pdf)
           lis=pdf.replace(" ", "=")
           return render_template("docx.html", variable=lis)
    return render_template("index.html")


@app.route('/docx', methods=['GET', 'POST'])
def docx():
    if request.method=="POST":
        lis = request.form.get('filename', None)
        lis = lis.replace("=", " ")
        return send_file(lis, as_attachment=True)
    return render_template("index.html")

if __name__=="__main__":
    app.debug = True
    app.run()

There are two templates called index.html and docx.html in Templates directory, this is how it looks in Pycharm:

Program structure

Does anyone have an idea where i made an mistake? Thanks

Advertisement

Answer

You are passing parameters into the convert method like this:

result = convert(docx_file=input_file, pdf_file=output_file, pages=pages)

But the docx2pdf convert method is defined like this:

def convert(input_path, output_path=None, keep_active=False):

as you can see, there is no parameter called docx_file, so you get the error that this parameter was unexpected. To fix this, either get rid of the names altogether:

result = convert(input_file, output_file)

or use the correct parameter names:

result = convert(input_path=input_file, output_path=output_file)

I also see that you have passed a variable called pages. I’m not familiar with this library so I can’t be 100% sure, but I didn’t find a convert function with a parameter called pages anywhere.

Since you are in PyCharm, right click on the convert function and select Go to -> Declaration to see the method. You should be able to see what the function does. There is limited documentation on this library so you will have to rely on reading the code to understand.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement