I'm uploading multiple files to flask using a form, I'm getting the file objects in the flask backend without a problem but the issue is I want to read the PDF files to extract text from them. I can't do it on the file objects I received from the form, another method I thought of was saving the file in the local storage then read them again when I did that using file.save(path, filename) it created an empty text file with the name - filename.pdf
app=Flask(__name__)
#app.route('/')
def index():
return '''
<form method='POST' action='/saveData'>
<input type='file' name='testReport'>
<input type='submit'>
</form>
'''
#app.route('/saveData', methods=['POST'])
def saveData():
if 'testReport' in request.files:
testReport= request.files['testReport']
#This isn't working, a text file is saved with the same name ,ending in pdf
testReport.save(os.path.join(app.config['UPLOAD_FOLDER'], testReport.filename))
return f'<h1>File saved {testReport.filename}</h1>'
else:
return 'Not done'
How do we operate on PDF files after uploading them to flask ?
How do we operate on PDF files after uploading them to flask ?
You should treat them just like normal PDF files - if they were uploaded via Flask application or gathered using other method is irrelevant here. As you
want to read the PDF files to extract text from them.
you should use PDF text-extraction tool, for example pdfminer.six, as this is external module you need to install it first: pip install pdfminer.six
You can directly follow the flask own way as mentioned [here]
This easily works with pdfs. Just don't forget to include your extension in ALLOWED_EXTENSIONS
Related
I have one file stored in google drive which I download using:
url = 'https://docs.google.com/spreadsheets/d/ID/edit?usp=sharing'
output = 'file.xlsx'
gdown.download(url, output, quiet=False)
My goal is to make that file downloadable in a html file through a button using Google Drive as a backend. Is there any way to do that?
Summarizing:
Some user open an html file where there's a button to download some file placed in my drive.
the flask template I am thinking to use looks like:
from flask import Flask, Response
app = Flask(__name__)
#app.route("/")
def hello():
return '''
<html><body>
Hello. Click me.
</body></html>
'''
#app.route("/getPlotCSV")
def getPlotCSV():
# with open("outputs/Adjacency.csv") as fp:
# csv = fp.read()
excel = '1,2,3\n4,5,6\n'
return Response(
excel,
mimetype="xlsx",
headers={"Content-disposition":
"attachment; filename=file.xlsx"})
EDIT:
I mapped my html button with
"""<form action={url}> <input type=submit value=Go to Google/></form>"""
but it takes me directly to the spreadsheet in google sheets. How do I assign an action to that button so that the file is downloaded locally to the user?
Hi so basically I have a form that uploads a file
<input class="btn btn-light" type="file" id="file" name="h" accept=".py">
I'm using bottle, so essentially I want to get the contents of the file but not have to save it locally. Using with open requires a path which I don't have as it is not saved locally.
f = request.POST['file']
I get the file using the above.
Is there any way I could upload the file without having to save it locally?
Any help would be much appreciated, I just need the name of the file and the contents.
No need to save the file (or to use BytesIO as suggested in a comment). You can simply read its contents:
from bottle import Bottle, request
app = Bottle()
#app.post("/upload")
def upload():
f = request.POST["file_name"]
text = f.file.read()
return f"Read {len(text)} bytes from the uploaded file.\n"
app.run()
Output:
$ echo "This is the file to upload." > file.txt
$ curl http://127.0.0.1:8080/upload -F file_name=#file.txt
Read 28 bytes from the uploaded file.
I use restplus flask api . I want to upload jpg file then rename and save to files location. then save its url. I searched and found this code on https://flask-restplus.readthedocs.io/en/stable/parsing.html#file-upload, but I dont understand do_something_with_file statment in this code. could you help me?
from werkzeug.datastructures import FileStorage
upload_parser = api.parser()
upload_parser.add_argument('file', location='files',
type=FileStorage, required=True)
#api.route('/upload/')
#api.expect(upload_parser)
class Upload(Resource):
def post(self):
uploaded_file = args['file'] # This is FileStorage instance
url = do_something_with_file(uploaded_file)
return {'url': url}, 201
You can refer to flask original documentation for uploading files Uploading files
Basically, all you need is FileStorage.save() method to save uploaded file.
I'm trying to interpret an image and my code works for interpreting the image saved locally but I want to interpret the image that I upload to a form through POST without actually storing it locally. Is that possible?
I tried to use request.form['receipt-photo'] (receipt-photo is the name of the upload field)
text = image_to_string(Image.open(request.form['receipt-photo']))
but I got FileNotFoundError: [Errno 2] No such file or directory: 'receipt3.jpg'
I also tried
text = image_to_string(request.form['receipt-photo'])
Error I got.
pytesseract.pytesseract.TesseractError: (1, 'Tesseract Open Source OCR Engine v4.0.0 with Leptonica Error, cannot read input file /Users/alexmarginean/Documents/Projects/Weeper/receipt3.jpg: No such file or directory Error during processing.')
Then I searched online and I found something about converting to bytes but it didn't work for me
text = image_to_string(Image.open(BytesIO(request.form['receipt-photo'])))
Error I got: TypeError: a bytes-like object is required, not 'str'
This works (but it uses the locally stored image)
This was my code that was using the locally stored image
text = image_to_string(Image.open('receipt3.jpg'))
I expect my photo to be usable just from sending a POST request without saving the image locally.
I would really appreciate if someone could help me.
Create a folder where you want to store the uploaded files, let's call it /uploads
In your form create an upload field and button
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="receipt-photo">
<input type="submit" value="Upload">
</form>
Then in Flask (assuming you use flask) add an endpoint
#app.route('/upload', methods=[POST])
file = request.files['receipt-photo']
f = os.path.join('/uploads', file.filename)
file.save(f)
# Manipulate image with PIL
I am using a Flask application to update some PDF files, convert them to an Excel file and send this file back to the user. I am using an instance folder to store the pdf and the excel files.
But when the user press the button "Download" in order to download the generated Excel file, an old file is downloaded (from an older session).
Moreover, when I try to change my code, for example, I changed the name of this Excel file: I can see the new name in the instance folder, but when I download the file with the webapp, it is still the old name (and old file). I have no idea where the webapp is looking for this old file...
MEDIA_FOLDER = '/media/htmlfi/'
app = Flask(__name__)
app.config.from_object(Config)
INSTANCE_FOLDER = app.instance_path
app.config['UPLOAD_FOLDER'] = INSTANCE_FOLDER+MEDIA_FOLDER
#app.route('/file/')
def send():
folder = app.config['UPLOAD_FOLDER']
try:
return send_file(folder+ "file.xlsx", as_attachment=True)
finally:
os.remove(folder+ "file.xlsx")
<a href="{{ url_for('send') }}" ><button class='btn btn-default'>DOWNLOAD</button></a>
I am really new to webapp in general, thank you for your help :)
send_file takes a cache_timeout parameter which is the number of seconds you want to cache the download. By default is 12 hours.
return send_file(
file.file_path(),
as_attachment=True,
cache_timeout=app.config['FILE_DOWNLOAD_CACHE_TIMEOUT'],
attachment_filename=file.file_name
)
http://flask.pocoo.org/docs/1.0/api/