JonathanHolvey / sharepy

Simple SharePoint authentication for Python
GNU General Public License v3.0
175 stars 52 forks source link

sharepy - read file instead of download file #35

Closed sbi-rviot closed 4 years ago

sbi-rviot commented 5 years ago

Hello,

I cannot find, using this library Sharepy, which I find so far very useful, a way to read a file (meaning its content) instead of downloading it.

Is there an equivalent to "getfile" that would be something like "readfile". I have seen the following functions that are built in Sharepy session but it doesn't seem to do the trick _( _init, _spauth, redigest, save, post, getfile, buildcookie).

I can give piece of code if the description of my problem is not clear enough. Thank you

JonathanHolvey commented 5 years ago

You should be able to simple use the get method to download a file as a string. The only difference between this and getfile is that the latter streams the output to disk, without having to load the whole response into memory first. If you're reading the file into a variable then I think using get should be fine.

import sharepy
s = sharepy.connect("example.sharepoint.com")
r = s.get("https://example.sharepoint.com/Library/Test%20File.pdf")
print(r.text)

If you want to use the file as a Python IO object, as you would when loading a file from disk, you can use the IO module:

import io
f = io.BytesIO(r.content)
sbi-rviot commented 5 years ago

Thank you for labeling my issue and providing me with an answer.

It doesn't seem to work however.

I have an excel file on sharepoint and here is what I do:

connection = sharepy.connect(url, username=username, password=password) site = url + sub_url source = "https://" + connection.site + "/sites/Connection/Shared Documents/file_name.xlsx" r = connection.getfile(source)

and I get the following message:

RuntimeError: The content for this response was already consumed

Any other idea?

Note: the file used is a standard excel file, only one tab, it reads without problem when I do the following (which implies I download it at first):

connection = sharepy.connect(url, username=username, password=password) site = url + sub_url source = "https://" + connection.site + "/sites/Connection/Shared Documents/file_name.xlsx" connection.getfile(source) df = pd.read_excel("file_name.xlsx")

Thank you so much!

JonathanHolvey commented 5 years ago

Sorry, my example should have used s.get() not s.getfile(). I've corrected this mistake now. Please try again.

sbi-rviot commented 5 years ago

Hello, Getting closer but still stuck. Very sorry about that. My guess is that my problem is due to the fact that the default encoding is chardet when what I would need utf-8.

print(r.text)

Now I get something in response but I can't see what it is :) and I find no way to convert this to a dataframe.

f = io.BytesIO(r.content)

Now I get something in response but it is not human readable :) and I find no way to convert this to a dataframe.