Hi is it possible to add a http header to a pdf file like Content-Disposition:attachment;filename=test.pdf in python, for now what i do is i send my pdf files to Amazons S3 by adding
--add-header=Content-Disposition:attachment;filename=test.pdf
but i would like to set this in python
Do you mean you want to upload files to S3 using Python and set some extra headers along the way?
If so, you could use simples3 library. Or some other similar library. I'll cover using simples3:
Follow the docs on how to instantiate a S3Bucket object. Once you have that, you can do:
bucket.put('path/to/your/pdf/object/on/s3',
pdf_object_bytes,
headers={'Content-Disposition': 'attachment;filename=test.pdf'})
Tried writing to a file or parsing everything as a string?
Related
I have a DataFrame that I would like to store as a CSV file in a Sharepoint.
It seems that the only way is to first save CSV file locally and then, using Shareplum, upload file to Sharepoint.
Is there a way to directly save DataFrame into Sharepoint as CSV file, without creating a local file?
Thanks a lot for your help.
It should be possible to write the csv content to an in-memory text buffer (e.g. StringIO or ByteIO) rather than to a local file - here is an example (last section of the page).
After that, you could use a library for writing the content directly to a Sharepoint: This discussion shows several approaches how to do that, including the Office365-REST-Python-Client and also SharePlum, which you have already mentioned.
Here are two more sources (Microsoft technical doc) that you might find useful:
How can I upload a file to Sharepoint using Python?
How to get and upload files from sharepoint with python?
I have a url, which downloads a zip file, containing a csv file. I want to parse the data and import it to local Database(sqlite) in Django.
In brief, input = url, pre-processing download .zip-> convert to .csv, output=csv rows in DB which has columns of csv as fields.
Well, I think if you google a bit you can do it yourself. I will give you the keyword:
For download a file you can you use request:
import requests
url = 'https://www.facebook.com/favicon.ico'
r = requests.get(url,
allow_redirects=True)
open('facebook.ico', 'wb').write(r.content)
To parse a csv file use xlsxwriter
To save the data to database, I suggest you save the data to django model then call model.save()
To use django on standalone script read this
How to import Django Settings to python standalone script
To use it as a service you should use rest_framework and write a custom viewset like this. I had quite a hard time get familiar with it, so good luck, but when you get long, drf is quite handy tool for everything, just not fast.
https://medium.com/django-rest-framework/django-rest-framework-viewset-when-you-don-t-have-a-model-335a0490ba6f
You should django settings to save the path to temp file. In linux you can use /tmp
I would like to produce some custom output with python with data from Tableau files. I dont have access to the Tableau server to run the 'Tabpy' library.
Is there any other way to do it?
Thank you in advance
You may find the following link useful.
https://community.tableau.com/thread/152463
One of the posts in the thread mentioned the following which is worth exploring:
If you're looking to generate a TWBX dynamically, you should rename
your .twbx file to .zip, extract the contents and you can do whatever
you want with those in Python to dynamically create or adjust a
workbook file. The structure / definition of the workbook file is just
XML so no special code needed to read and parse that.
I have a URL in a web analytics reporting platform that basically triggers a download/export of the report you're looking at. The downloaded file itself is a CSV, and the link that triggers the download uses several attached parameters to define things like the fields in the report. What I am looking to do is download the CSV that the link triggers a download of.
I'm using Python 3.6, and I've been told that the server I'll be deploying on does not support Selenium or any webkits like PhantomJS. Has anyone successfully accomplished this?
If the file is a CSV file, you might want to consider downloading it's content directly, by using the requests module, something like this.
import requests
session=requests.Session()
information=session.get(#the link of the page here)
Then You can decode the information and read the contents as you wish using the CSV module, something like this (the csv module should be imported):
decoded_information=information.content.decode('utf-8')
data=decoded_information.splitlines()
data=csv.DictReader(data)
You can use a for loop to access each row in the data as you wish using the column headings as dictionary keys like so:
for row in data:
itemdate=row['Date']
...
Or you can save the decoded contents by writing them to a file with something like this:
decoded_information=information.content.decode('utf-8')
file=open("filename.csv", "w")
file.write(decoded_information)
file.close
A couple of links with documentation on the CSV module is provided here just in case you haven't used it before:
https://docs.python.org/2/library/csv.html
http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python/
Hope this helps!
Need to develop a project for file uploading and generating its(files's) URL, which could be shared. Are there any particular libraries or simple means in Python,(Django) that would be handy and efficient.?
~ Newbie trying a Herculean Task~ Thanks in Advance :)
Much of this is done for you in the Django-backed version of this excellent JQuery file upload app.
Try out a demo of the original here.
No. Since the Storage used may not even save the actual files on the filesystem, there is no universal way to generate a URL to them. You will need to create a view that gets passed a key that it can use to identify the record that has the file field, then you will need to respond with the file contents yourself.