转换docx2pdf时fileNotfound错误

发布于 2025-02-11 16:09:14 字数 2146 浏览 0 评论 0原文

我试图制作一个Python程序,该程序将DOCX文件转换为PDF使用烧瓶。但是我得到这样的错误:

下面我粘贴代码:

from flask import Flask
from flask import request, render_template, send_file
import os
from typing import Tuple
from docx2pdf import convert

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(input_file, output_file)
           print('Convert Done')
           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(" ", "=")
           os.rename("hello.pdf", lis)
           print('converted')
           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()

有人可以帮助吗?这是我的最后一篇文章,以获取更多详细信息: filenotfounderror docx to pdf转换程序

im trying to make a python program that converts docx files to pdf using flask. But i get an Error like this:
enter image description here

Below I paste code:

from flask import Flask
from flask import request, render_template, send_file
import os
from typing import Tuple
from docx2pdf import convert

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(input_file, output_file)
           print('Convert Done')
           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(" ", "=")
           os.rename("hello.pdf", lis)
           print('converted')
           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()

Can any one help? This is my last post for further details:
FileNotFoundError docx to pdf convert program

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄟ。诗瑗 2025-02-18 16:09:15

如我们所见,

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(" ", "=")
    os.rename("hello.pdf", lis)
    print('converted')
    return render_template("docx.html", variable=lis)

您尝试转换文件。但是您使用docx2pdf(input_file,output_file)。而且,如果您查看“导入部分”,则可以看到此信息

from docx2pdf import convert

,因此您不使用转换 - >文件未保存和处理 - > filenotfounderror

As we can see there

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(" ", "=")
    os.rename("hello.pdf", lis)
    print('converted')
    return render_template("docx.html", variable=lis)

You try to convert file. But you use docx2pdf(input_file, output_file). And if you look at the import section, you can see this

from docx2pdf import convert

So, you don't use convert -> file not saved and processed -> FileNotFoundError

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文