I'm trying to get my form data to be captured and written to a word document. I have the following function...
def index():
FILE_TYPES = set(['txt', 'doc', 'docx', 'odt', 'pdf', 'rtf', 'text', 'wks', 'wps', 'wpd'])
mail = Mail(app)
errors = ''
form = ApplicationForm(CombinedMultiDict((request.files, request.form)))
capture = [form.department.data, form.name.data, form.address.data, str(form.telephone.data), form.email.data, form.explain.data]
department_data = form.department.data
name_data = form.name.data
if department_data == 'cpoms':
flash(capture)
if form.validate_on_submit():
flash('Thanks %s, we will try to get back to your regarding you application as soon as possible.' % form.name.data)
print "Form successfully submitted"
submit_name = form.file_upload.data.filename
if '.' in submit_name and submit_name.rsplit('.', 1)[1] in FILE_TYPES:
filename = secure_filename(submit_name)
form.file_upload.data.save('uploads/' + filename)
return redirect('home')
Does anyone know how I can move the data from the capture variable and write this to a text document?
How about:
with open("/path/to/file.txt", "a") as write_file:
for cap in capture:
write_file.write(capt)
I hope this will work.
Related
I'm writing a flask API to extract text from the document. I want to check the extension and if it is pdf I'll give it to the pdf miner else docx2txt.
#app.route('/text-extraction', methods = ['POST'])
def text_extractions():
f = request.files['files']
split_tup = os.path.splitext(f)
file_extension = split_tup[1]
if file_extension == '.pdf':
return extract_text(f)
else:
text = docx2txt.process(f)
if extract_text:
return text.replace('\t', ' ')
return None
I want save document when uploaded and run pandas script and save that script but also to forward to user do download it. How to do it simple way?
This is how I tried to do it, upload and save upload works, but pandas script is not working.
def my_view(request):
message = 'Upload as many files as you want!'
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile=request.FILES['docfile'])
newdoc.save()
#This part is doing calculations for uploaded file
dfs = pd.read_excel(newdoc, sheet_name=None)
with pd.ExcelWriter('output_' + newdoc + 'xlsx') as writer:
for name, df in dfs.items():
print(name)
data = df.eval('d = column1 / column2')
ooutput = data.eval('e = column1 / d')
ooutput.to_excel(writer, sheet_name=name)
output = io.BytesIO()
writer = pd.ExcelWriter(output, engine='xlsxwriter')
newdoc.to_excel(writer, index=False)
writer.save()
output.seek(0)
response = HttpResponse(output,
content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
response['Content-Disposition'] = 'attachment; filename=%s.xlsx' % 'Download'
return response
return redirect('results')
else:
message = 'The form is not valid. Fix the following error:'
else:
form = DocumentForm()
documents = Document.objects.all()
context = {'documents': documents, 'form': form, 'message': message}
return render(request, 'list.html', context)
def results(request):
documents = Document.objects.all()
context = {'documents': documents}
return render(request, 'results.html', context)
dfs = pd.read_excel(newdoc, sheet_name=None)
You are passing newdoc here, which is your model, not a IO object.
Try
dfs = pd.read_excel(newdoc.docfile, sheet_name=None)
or
dfs = pd.read_excel(newdoc.docfile.file, sheet_name=None)
This is the not answer but my suggestion, you are following the wrong way of implementation.
You can set background process in Celery
After the job is done in Celery you can mail/notify the User with download URL.
The above given solution will be long but scalable and performance-oriented as well.
I am new to python and I want to use the results from the OCR (string) to be able to match the first column of my csv file and then only if the condition is true (the string from ocr matches to the one in the csv then it should use the pic. I get an error as soon as I try to integrate the code together.
For the OCR, I am using pytesseract and I am using Flask to render the web app.
The error I get is : AttributeError: '_io.TextIOWrapper' object has no attribute 'filename'
New error: The view function for 'upload_image' did not return a valid response. The function either returned None or ended without a return statement.
This error only persists when I try to add this code:
match = extracted_text
matched_row = None
with open("/Users/ri/Desktop/DPL/DPL.csv", "r") as file:
# Read file as a CSV delimited by tabs.
reader = csv.reader(file, delimiter='\t')
for row in reader:
if row[0] == match:
matched_row = row
print(matched_row)
app.py
#app.route('/', methods=['POST'])
def upload_image():
if request.method == 'POST':
# checks whether or not the post request has the file part
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No file selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(os.getcwd() +
UPLOAD_INPUT_IMAGES_FOLDER, file.filename))
flash('File successfully uploaded')
# calls the ocr_processing function to perform text extraction
extracted_text = ocr_processing(file)
print(extracted_text)
match = extracted_text
matched_row = None
with open("/Users/ri/Desktop/DPL/DPL.csv", "r") as f:
# Read file as a CSV delimited by tabs.
reader = csv.reader(f, delimiter='\t')
for row in reader:
if row[0] == match:
matched_row = row
print(matched_row)
loaded_vec = CountVectorizer(
vocabulary=pickle.load(open("./tfidf_vector.pkl", "rb")))
loaded_tfidf = pickle.load(open("./tfidf_transformer.pkl", "rb"))
model_pattern_type = pickle.load(
open("./clf_svm_Pattern_Category.pkl", "rb"))
model_pattern_category = pickle.load(
open("./clf_svm_Pattern_Type.pkl", "rb"))
match = [match]
X_new_counts = loaded_vec.transform(
match)
# .values.astype('U')
X_new_tfidf = loaded_tfidf.transform(X_new_counts)
predicted_pattern_type = model_pattern_type.predict(X_new_tfidf)
your_predicted_pattern_type = predicted_pattern_type[0]
predicted_pattern_category = model_pattern_category.predict(
X_new_tfidf)
your_predicted_pattern_category = predicted_pattern_category[0]
return render_template('uploads/results.html',
msg='Processed successfully!',
match=match,
your_predicted_pattern_category=your_predicted_pattern_category,
your_predicted_pattern_type=your_predicted_pattern_type,
img_src=UPLOAD_INPUT_IMAGES_FOLDER + file.filename)
# break
else:
print("no mattern found")
else:
flash('Allowed file types are txt, pdf, png, jpg, jpeg, gif')
return redirect(request.url)
first python script generating folder based on request. second script giving similar image and saving in generated folder. While in multiple request, images is not saving as per request.Only images saving in first folder generated. for example if three concurrent request came it generated 3 folders.but all result is saving in first folder other 2 folders is empty.so second and third user not getting result.
1st Script
#app.route('/imageUploads', methods=['GET', 'POST'])
def upload_img():
import secrets
import string
N = 9
res = ''.join(secrets.choice(string.ascii_uppercase + string.digits)
for i in range(N))
print("The generated random string : " + str(res))
timesearch = str(res)
randomvariable = timesearch
result = 'static/' + randomvariable
globe = result;
if not gfile.Exists(result):
result = 'static/' + randomvariable
os.mkdir(result)
#shutil.rmtree(result)
else:
for root, dirs, files in os.walk(result):
for file in files:
os.remove(os.path.join(root, file))
if request.method == 'POST' or request.method == 'GET':
print(request.method)
# check if the post request has the file part
if 'file' not in request.files:
print('No file part')
return redirect(request.url)
file = request.files['file']
#print(file.filename)
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
print('No selected file')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
#filename = file.filename
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
inputloc = os.path.join(app.config['UPLOAD_FOLDER'], filename)
recommend(inputloc, extracted_features,randomvariable)
#os.remove(inputloc)
#label = label1.replace("\n", "")
# name = os.path.basename(pred_final[neighbor])
image_path = randomvariable
image_list =[os.path.join(image_path,file) for file in os.listdir(result)
if not file.startswith('.')]
image_list = [k.replace("\\","/") for k in image_list]
)
images = []
resultImages = []
for i, image in enumerate(image_list):
v = len(image_list)
print(v,"number of images are")
images.append({'path': 'https://localhost:5000/'+image, 'fileName': os.path.basename(image)})
resultImages.append(os.path.basename(image))
with open('data.json', "w") as f:
json.dump({"resultImages": resultImages}, f)
2nd python script
def get_top_k_similar(image_data, pred, pred_final, k=10):
result = 'static/' + recommend.randomString
# cosine calculates the cosine distance, not similiarity. Hence no need to reverse list
top_k_ind = np.argsort([cosine(image_data, pred_row) \
for ith_row, pred_row in enumerate(pred)])[:k]
print(top_k_ind)
for i, neighbor in enumerate(top_k_ind):
image = ndimage.imread(pred_final[neighbor])
name = os.path.basename(pred_final[neighbor])
name = os.path.join('static', recommend.randomString, name)
imsave(name, image)
I'm building a web app in Django.
I have a form that sends a file to views.py.
Views:
#login_required(login_url=login_url)
def addCancion(request):
if request.method == 'POST':
form2 = UploadSong(request.POST, request.FILES)
if form2.is_valid():
if(handle_uploaded_song(request.FILES['file'])):
path = '%s' % (request.FILES['file'])
ruta = "http://domain.com/static/canciones/%s" % path
usuario = Usuario.objects.get(pk=request.session['persona'])
song = Cancion(autor=usuario, cancion=ruta)
song.save()
return HttpResponse(ruta)
else:
return HttpResponse("-3")
else:
return HttpResponse("-2")
else:
return HttpResponse("-1")
I'm trying to upload only the MP3 files, but I don't know how to make this filter.
I tried a class named "ContentTypeRestrictedFileField(FileField):" and doesn't work.
How can I get the file type in views.py?
Thanks!
You could also use the clean() method from the form, which is used to validate it. Thus, you can reject files that are not mp3. Something like this:
class UploadSong(forms.Form):
[...]
def clean(self):
cleaned_data = super(UploadSong, self).clean()
file = cleaned_data.get('file')
if file:
filename = file.name
print filename
if filename.endswith('.mp3'):
print 'File is a mp3'
else:
print 'File is NOT a mp3'
raise forms.ValidationError("File is not a mp3. Please upload only mp3 files")
return file
with import mimetypes, magic:
mimetypes.MimeTypes().types_map_inv[1][
magic.from_buffer(form.cleaned_data['file'].read(), mime=True)
][0]
gives you the extension as '.pdf' for example
https://docs.djangoproject.com/en/dev/topics/forms/#processing-the-data-from-a-form
http://docs.python.org/2/library/mimetypes.html#mimetypes.MimeTypes.types_map_inv
https://github.com/ahupp/python-magic#usage
for get direct of request:
import os
extesion = os.path.splitext(str(request.FILES['file_field']))[1]
or get extesion in db - model.
import os
file = FileModel.objects.get(pk=1) # select your object
file_path = file.db_column.path # db_column how you save name of file.
extension = os.path.splitext(file_path)[1]
You mean this:
u_file = request.FILES['file']
extension = u_file.split(".")[1].lower()
if(handle_uploaded_song(file)):
path = '%s' % u_file
ruta = "http://example.com/static/canciones/%s" % path
usuario = Usuario.objects.get(pk=request.session['persona'])
song = Cancion(autor=usuario, cancion=ruta)
song.save()
return HttpResponse(content_type)
You can use request.FILES["file_field_name"].content_type
my_file = request.FILES["file_field_name"]
if my_file.content_type != 'text/csv':
print("Your file must be a CSV type")
Using FileType.py library.
Example:
kind = filetype.guess('tests/fixtures/sample.jpg')
if kind is None:
print('Cannot guess file type!')
return
print('File extension: %s' % kind.extension)
print('File MIME type: %s' % kind.mime)
Using MimeTypes().guess_extension() method. Check snippet below.
# guess the file extension
file_obj.seek(0)
mime = magic.from_buffer(file_obj.read(), mime=True)
extension = mimetypes.MimeTypes().guess_extension(mime)
>>> print extension
.jpeg