0

I have a question.I want use python module pdfplumber to do pdf read,and transfer file to excel,and save it.But this program doesn't seem to be very successful.Hope someone can give me some pointers

from flask import Flask, request, render_template
import pdfplumber
from openpyxl import Workbook
import os


app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        # 獲取上傳的 PDF 文件
        uploaded_file = request.files['file']

        if uploaded_file.filename != '':
            # 保存上傳的 PDF 文件
            pdf_filename = os.path.join('uploads', uploaded_file.filename)
            uploaded_file.save(pdf_filename)

            # 提取 PDF 文本内容
            pdf_text = extract_pdf_text(pdf_filename)

            if pdf_text:
                # 創建Excel文件並寫入PDF内容
                excel_filename = os.path.join('downloads', f'{os.path.splitext(uploaded_file.filename)[0]}.xlsx')
                create_excel_file(excel_filename, pdf_text)

                return render_template('index.html', message='轉換成功', excel_link=excel_filename)
            else:
                return render_template('index.html', error='無法讀取PDF')

    return render_template('index.html')

def extract_pdf_text(pdf_filename):
    pdf_text = ''
    with pdfplumber.open(pdf_filename) as pdf:
        for page in pdf.pages:
            pdf_text += page.extract_text()

    return pdf_text

def create_excel_file(excel_filename, pdf_text):
    workbook = Workbook()
    sheet = workbook.active

    for line in pdf_text.split('\n'):
        sheet.append([line])

    workbook.save(excel_filename)

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

result(terminal) PS C:\python-flask> python app.py

  • Serving Flask app 'app'
  • Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Running on http://127.0.0.1:5000 Press CTRL+C to quit
  • Restarting with stat
  • Debugger is active!
  • Debugger PIN: 782-820-844

result (transfer file web page) enter image description here

I try this way,but still haven't result.

from flask import Flask

app = Flask(__name__)

@app.route("/")
def index():
    return "<h1>Hello!</h1>"

if __name__ == "__main__":
    from waitress import serve
    serve(app, host="0.0.0.0", port=8080)
gary
  • 1
  • 1
  • The index.html might also be helpful for those looking at this issue. – moken Aug 17 '23 at 11:17
  • try to change `pdf_text += page.extract_text()` with `pdf_text = page.extract_text()` – Herojos Aug 18 '23 at 10:00
  • I try pdf_text = page.extract_text() ,still can't be useful,terminal show TemplateNotFound jinja2.exceptions.TemplateNotFound: index.html – gary Aug 28 '23 at 05:58
  • I checked 'index.html' ,how can i fix this problem – gary Aug 28 '23 at 06:02
  • In `C:\python-flask`, do you have the "uploads" directory? Can also refer to the code at https://flask.palletsprojects.com/en/2.3.x/patterns/fileuploads/ that specifies the upload directory in the app config. – Samkit Jain Sep 02 '23 at 12:13

0 Answers0