multiple openpyxl xlsx workbooks into one .zip file for download - django

I am trying to get some xlsx files from a form, i load them using openpyxl and do some data processing.. and finally i need to download all processed xlsx files zipped to the user.
here is an example of what i did so far
if form.is_valid():
s = StringIO.StringIO()
zf = zipfile.ZipFile(s, mode="w")
for xlsx in request.FILES.getlist('xlsxs'):
element_column = "G"
element_row = 16
massar_column = "C"
massar_row_start = 18
loop = column_index_from_string(element_column)
while (loop <= ws.max_column):
for i in range(massar_row_start, ws.max_row+1):
# ...
ws["%s%s" % (element_column,i)] = 0
# ...
loop+=2
element_column = get_column_letter(loop)
buf = save_virtual_workbook(wb)
zf.write(buf) # or zf.write(wb)
zf.close()
response = HttpResponse(s.getvalue(), content_type="application/x-zip-compressed")
response['Content-Disposition'] = "attachment; filename=notes.zip"
return response
I get the error
TypeError at My_view
stat() argument 1 must be encoded string without null bytes, not str
Thanks in advance for any help you can offer.

save_virtual_workbook returns a bytestream - source.
You are passing this value to ZipFile.write which is expecting a filename.
I think you should be using ZipFile.writestr, and you need to provide a filename that will be used inside the archive. I'm not sure how you are getting the error message you see, but this is the first mistake I can see.

Related

Get files from Aw3 and append them in a .zip in Django

I would like to download some files from AWS S3 in a .zip, with all these files.
I am using django==2.2.1 with boto3 library, last version.
I am trying to get the files one by one, convert them with json.dump and insert them in a .zip file with generate_zip function.
Not resolve nothing, I mean, it not resolve any error and it not response the .zip or something in console that I would see. Probably the .zip was not created, but I do not know because, like I told before, it not response nothing.
My code is:
for id_file in files:
BUCKET_NAME="xxxx"
BUCKET_FILE_NAME='folder/'+id_file+'/'+category+"/"+name_file
s3 = boto3.client('s3',aws_access_key_id=settings.AWS_ACCESS_KEY_ID,aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,region_name='eu-west-2')
s3_response_object=s3.get_object(Bucket=BUCKET_NAME,Key=BUCKET_FILE_NAME)
s3_response_object['Body'] = s3_response_object['Body'].read().decode("ISO-8859-1")
s3_response_object['Key'] = settings.AWS_ACCESS_KEY_ID+'/'+settings.AWS_SECRET_ACCESS_KEY
location = s3.get_bucket_location(Bucket=BUCKET_NAME)['LocationConstraint']
url = "https://s3-%s.amazonaws.com/%s/%s" % (location, BUCKET_NAME, BUCKET_FILE_NAME)
s3_response_object['URL'] = url
file_data = json.dumps(s3_response_object, default = myconverter)
resultFiles.append(file_data)
full_zip_in_memory = generate_zip(resultFiles)
response = HttpResponse(full_zip_in_memory, content_type='application/x-zip')
response['Content-Disposition'] = 'attachment; filename="files.zip"'
return response
La función generate_zip
def generate_zip(files):
mem_zip = BytesIO()
with zipfile.ZipFile(mem_zip, mode="w",compression=zipfile.ZIP_DEFLATED) as zf:
for f in files:
zf.writestr(f[0], f[1])
return mem_zip.getvalue()

Save a file from requests using django filesystem

I'm currently trying to save a file via requests, it's rather large, so I'm instead streaming it.
I'm unsure how to specifically do this, as I keep getting different errors. This is what I have so far.
def download_file(url, matte_upload_path, matte_servers, job_name, count):
local_filename = url.split('/')[-1]
url = "%s/static/downloads/%s_matte/%s/%s" % (matte_servers[0], job_name, count, local_filename)
with requests.get(url, stream=True) as r:
r.raise_for_status()
fs = FileSystemStorage(location=matte_upload_path)
print(matte_upload_path, 'matte path upload')
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
fs.save(local_filename, f)
return local_filename
but it returns
io.UnsupportedOperation: read
I'm basically trying to have requests save it to the specific location via django, any help would be appreciated.
I was able to solve this, by using a tempfile to save the python requests, then saving it via the FileSystemStorage
local_filename = url.split('/')[-1]
url = "%s/static/downloads/%s_matte/%s/%s" % (matte_servers[0], job_name, count, local_filename)
response = requests.get(url, stream=True)
fs = FileSystemStorage(location=matte_upload_path)
lf = tempfile.NamedTemporaryFile()
# Read the streamed image in sections
for block in response.iter_content(1024 * 8):
# If no more file then stop
if not block:
break
# Write image block to temporary file
lf.write(block)
fs.save(local_filename, lf)

How to download data from azure-storage using get_blob_to_stream

I have some files in my azure-storage account. i need to download them using get_blob_to_stream.it is returning azure.storage.blob.models.Blob object. so i couldn't download it by using below code.
def download(request):
file_name=request.POST['tmtype']
fp = open(file_name, 'wb')
generator = block_blob_service.list_blobs(container_name)
for blob in generator:
print(blob.name)
if blob.name==file_name:
blob=block_blob_service.get_blob_to_stream(container_name, blob.name, fp,max_connections= 2)
response = HttpResponse(blob, content_type="image/png")
response['Content-Disposition'] = "attachment; filename="+file_name
return response
You can actually use the get_blob_to_path property, below is an example in python:
from azure.storage.blob import BlockBlobService
bb = BlockBlobService(account_name='', account_key='')
container_name = ""
blob_name_to_download = "test.txt"
file_path ="/home/Adam/Downloaded_test.txt"
bb.get_blob_to_path(container_name, blob_name_to_download, file_path, open_mode='wb', snapshot=None, start_range=None, end_range=None, validate_content=False, progress_callback=None, max_connections=2, lease_id=None, if_modified_since=None, if_unmodified_since=None, if_match=None, if_none_match=None, timeout=None)
This example with download a blob file named: "test.txt", in a container, to File_path"/home/Adam/Downloaded_test.txt" , you can also keep the same name if you'd like to as well. You can find more samples including this one in https://github.com/adamsmith0016/Azure-storage
If you want to use get_blob_to_stream. You can download with below code:
with io.open(file_path, 'wb') as file:
blob = block_blob_service.get_blob_to_stream(
container_name=container_name,
blob_name=blob_name, stream=file,
max_connections=2)
Just note that the file content will be streamed to the file rather than the returned blob object. The blob.content should be None. That is by design. See https://github.com/Azure/azure-storage-python/issues/538.

Django create dynamic txt file and download with special file name

I am trying create dynamic text file and download it when my method called.I used steps at here.But when I change the file name, file isn't downloaded, displayed on browser. What can I do ? My code is below. Thanks for advice.
def view_method(request):
file_name = 'students.txt'
lines = []
data = Student.objects.all()
for d in data:
lines.append('{0};{1};{2}'.format(d.name,d.surname,d.amount))
response_content = '\n'.join(lines)
response = HttpResponse(response_content, content_type="text/plain,charset=utf8")
response['Content-Disposition'] = 'attachment; filename={0}'.format(file_name)
return response
I solved this problem with correct the file name chars.My file name contains utf-8 characters and points.

extract data from a uploaded pdf using django.core.files.uploadedfile.InMemoryUploadedFile returns encoded data

I am uploading a pdf using Djnago 1.10.4 framework,
uploaded file returns objects of django.core.files.uploadedfile.InMemoryUploadedFile
When I am trying to extract the data from the object using type(file_uploaded.file)
I am getting
object.
now using file_uploaded.file.read().decode('utf-8') should give me the extracted data from pdf. But I am getting encrypted data in hexadecimal or some thing
if request.FILES:
file_uploaded = request.FILES["file"]
# file_uploaded = file.name
print('file_uploaded')
print(file_uploaded.file.read().decode('UTF-8', errors="ignore"))
print("file_uploaded.file", type(file_uploaded.file))
print("file_uploaded", type(file_uploaded))
file_type = ""
extn = file_uploaded.name
file_supported = [".pdf", ".jpg", ".png",".json", ".rtf", ".docx", ".txt", ".csv"]
extension = '.'+extn.split(".")[1]
# extension = "." + .split(".")[1]
if(extension in file_supported):
for formates in file_supported:
if (formates == extension):
file_type = formates[1:]
else:
print ('file is not uploaded')
above code workes for text files like csv, rft but not working for pdf. Output from pdf file is this
Β-'EMhXmt99*6":ȩ#+6H:yVl=d|9p
PeǸP|PRMYy+zR!ȷ/,yL=rD>lRM_QhOYO&|C'x.1%V]!pK,KEq㠪HSYO_O
{
4^R ς~7އG|䡚wgn˒( pOIUk]CF<~ (bt
譌)0vפ/HgDh+刃gzDU;$k-XOeki~kgۍJ_v41"
WS(X ]|GQyoZ?uu`^a;v(aiky|RUUSVT#P֯׹PIAZIW-)Ʌb?^mᇏ
UQbq3D#_J^Hdp7e,2̓\r|/e,vlBHG`-#!K<x1~i>s#cN7iL7QON-Wؖ
following is the output from my terminal when csv file uploaded:
file_uploaded
3526 HIGH ST,SACRAMENTO,95838,CA,2,1,836,Residential
file_uploaded.file <class '_io.BytesIO'>
file_uploaded
<class'django.core.files.uploadedfile.InMemoryUploadedFile'>
Please help me on this.