sschet commited on
Commit
de1bffc
1 Parent(s): da17f51

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -21
main.py CHANGED
@@ -1,37 +1,28 @@
1
  import os
2
  import glob
3
- import re
4
- import pandas as pd, numpy as np
5
  import comtypes.client
6
  import docx
7
- from docx.document import Document
8
- from docx.oxml.table import CT_Tbl
9
- from docx.oxml.text.paragraph import CT_P
10
- from docx.table import _Cell, Table
11
- from docx.text.paragraph import Paragraph
12
- from flask import Flask, request, send_file
13
- from werkzeug.utils import secure_filename
14
  import zipfile
15
  import io
16
 
17
- app = Flask(__name__)
18
 
19
  UPLOAD_FOLDER = 'uploads/'
20
  if not os.path.exists(UPLOAD_FOLDER):
21
  os.makedirs(UPLOAD_FOLDER)
22
 
23
- @app.route('/convert', methods=['POST'])
24
- def convert_pdfs():
25
- if 'pdf_files' not in request.files:
26
- return 'No file part', 400
27
-
28
- pdf_files = request.files.getlist('pdf_files')
29
  if not pdf_files:
30
- return 'No selected file', 400
31
 
32
  for pdf_file in pdf_files:
33
- filename = secure_filename(pdf_file.filename)
34
- pdf_file.save(os.path.join(UPLOAD_FOLDER, filename))
 
35
 
36
  path_pdf = UPLOAD_FOLDER
37
  path_docx = UPLOAD_FOLDER
@@ -58,7 +49,8 @@ def convert_pdfs():
58
  zf.write(filepath, filename)
59
 
60
  memory_file.seek(0)
61
- return send_file(memory_file, attachment_filename='converted_docx.zip', as_attachment=True)
62
 
63
  if __name__ == '__main__':
64
- app.run(host='0.0.0.0', port=5000)
 
 
1
  import os
2
  import glob
 
 
3
  import comtypes.client
4
  import docx
5
+ from fastapi import FastAPI, File, UploadFile
6
+ from pydantic import BaseModel
7
+ from starlette.responses import FileResponse
 
 
 
 
8
  import zipfile
9
  import io
10
 
11
+ app = FastAPI()
12
 
13
  UPLOAD_FOLDER = 'uploads/'
14
  if not os.path.exists(UPLOAD_FOLDER):
15
  os.makedirs(UPLOAD_FOLDER)
16
 
17
+ @app.post('/convert')
18
+ async def convert_pdfs(pdf_files: list[UploadFile] = File(...)):
 
 
 
 
19
  if not pdf_files:
20
+ return {"detail": "No selected file"}
21
 
22
  for pdf_file in pdf_files:
23
+ filename = pdf_file.filename
24
+ with open(os.path.join(UPLOAD_FOLDER, filename), "wb") as f:
25
+ f.write(await pdf_file.read())
26
 
27
  path_pdf = UPLOAD_FOLDER
28
  path_docx = UPLOAD_FOLDER
 
49
  zf.write(filepath, filename)
50
 
51
  memory_file.seek(0)
52
+ return FileResponse(memory_file, filename='converted_docx.zip', media_type='application/zip')
53
 
54
  if __name__ == '__main__':
55
+ import uvicorn
56
+ uvicorn.run(app, host='0.0.0.0', port=7860)