I am trying to read an excel file from SharePoint to python and I get the following error:
ValueError: Excel file format cannot be determined, you must specify an engine manually.
The python script goes something like that:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import io
import pandas as pd
url = "https://company.sharepoint.com/:x:/r/sites/A_team/"
username = 'myemail#mail.com'
password = 'password'
relative_url = "sites/A_team/Documents_python/Folder/Book.xlsx"
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()
print("Authentication successful")
else:
print(ctx_auth.get_last_error())
response = File.open_binary(ctx, relative_url)
bytes_file_obj = io.BytesIO()
bytes_file_obj.write(response.content)
bytes_file_obj.seek(0) #set file object to start
pd.read_excel(bytes_file_obj)
Does anyone have an idea what should I do?
I tried to add an engine, but then I get the following error:
BadZipFile: File is not a zip file
Related
How to Download a PDF File from Online SharePoint using python?
I've tried this code: but whenever I try this it downloads a broken file
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
url = "SharePointFilePath"
username= "Username"
password= "Password"
ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "SharepointURL")
with open("file.pdf","wb") as local_file:
local_file.write(response.content)
You need to write, not to read:
with open("file.pdf","wb") as local_file:
local_file.write(response.content)
Use another flag 'wb' instead of 'rb' and write method instead of read
with open('file.pdf', 'wb') as f:
f.write(response.content)
Read about flags
I need to download a file from sharepoint using user credentials (rather that client credentials).
I've tried this:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
root_url = "https://company-my.sharepoint.com"
full_url = "https://company-my.sharepoint.com/personal/Documents/AB.csv"
ctx = ClientContext(root_url)
ctx.with_user_credentials(<my_email>,
<my_password)
response = File.open_binary(ctx, full_url)
print(response.content)
The response I am getting is
*b'{"error":{"code":"-2147024809, System.ArgumentException","message":{"lang":"en-US","value":"serverRelativePath\\r\\nParameter
name: Specified value is not supported for the serverRelativePath
parameter."}}}'*
I am trying to access share point site as api using python to download files.
So far I have tried "shareplum" , "office365", and "sharepy" modules in python.
Every time I am getting authentication error due to invalid username or password.
But I can logon to that share point site from web browser using same username and password.
What approach should I follow or what sort of authentication should I use ?
Regards
Santanu
shareplum:
from shareplum import Site, Office365
from shareplum.site import Version
import json
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config_path = '\\'.join([ROOT_DIR, 'config_1.json'])
with open(config_path) as config_file:
config = json.load(config_file)
config = config['share_point']
USERNAME = config['user']
PASSWORD = config['password']
SHAREPOINT_URL = config['url']
SHAREPOINT_SITE = config['site']
SHAREPOINT_DOC = config['doc_library']
def auth():
authcookie = Office365(SHAREPOINT_URL, username=USERNAME, password=PASSWORD).GetCookies()
print(f"authcookie: {authcookie}")
site = Site(SHAREPOINT_SITE, version=Version.v365, authcookie=authcookie)
print(f"site: {site}")
return site
def connect_folder(folder_name):
auth_site = auth()
print(f"auth_site: {auth_site}")
sharepoint_dir = '/'.join([SHAREPOINT_DOC, folder_name])
print(f"sharepoint_dir: {sharepoint_dir}")
folder = auth_site.Folder(sharepoint_dir)
print(f"folder: {folder}")
return folder
def download_file(file_name, folder_name):
_folder = connect_folder(folder_name)
print(f"_folder: {_folder}")
return _folder.get_file(file_name)
def run():
# set file name
file_name = 'Hardy_ServiceReporting_Aug2021.csv'
# set the folder name
folder_name = 'Problem Management'
# get file
file = download_file(file_name, folder_name)
print(file)
if __name__ == "__main__":
run()
Error: Error from Office 365:', 'AADSTS50126: Error validating credentials due to invalid username or password.'
office365:
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import json
import os
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
config_path = '\\'.join([ROOT_DIR, 'config_1.json'])
with open(config_path) as config_file:
config = json.load(config_file)
config = config['share_point']
USERNAME = config['user']
PASSWORD = config['password']
SHAREPOINT_URL = config['url']
SHAREPOINT_SITE = config['site']
SHAREPOINT_DOC = config['doc_library']
FOLDER = "Problem Management"
url = SHAREPOINT_SITE + "/_api/web/GetFolderByServerRelativeUrl('{}/{}')/Files".format(SHAREPOINT_DOC, FOLDER)
print(f"url: {url}")
ctx_auth = AuthenticationContext(url)
print(f"ctx_auth: {ctx_auth}")
ctx_auth.acquire_token_for_user(USERNAME, PASSWORD)
ctx = ClientContext(url, ctx_auth)
print(f"ctx: {ctx}")
Error: An error occurred while retrieving token from XML response: AADSTS53003: Access has been blocked by Conditional Access policies. The access policy does not allow token issuance.
I have the following code that I believe would work for most sharepoint sites, but am receiving the following error:
Exception("Check username/password and rootsite")
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
url = 'https://company.sharepoint.com'
username = 'user123#company.com'
password = 'password'
listname = 'Test List'
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
sp_list = ctx.web.lists.get_by_title(listname)
items = sp_list.get_items()
ctx.load(items)
ctx.execute_query()
else:
print(ctx_auth.get_last_error())
How would I manipulate this code to ensure that I can pull data from a SharePoint List into Python?
I would suggest you check Office365-REST-Python-Client
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
ctx_auth = AuthenticationContext(url)
if ctx_auth.acquire_token_for_user(username, password):
ctx = ClientContext(url, ctx_auth)
web = ctx.web
sp_list = ctx.web.lists.get_by_title(listname)
items = sp_list.get_items()
ctx.load(items)
ctx.execute_query()
# to do
else:
print ctx_auth.get_last_error()
I am trying to upload my excel spreadsheet to a document library on my SharePoint Online site. The Sharepoint URL and the folder location on the SharePoint site are listed in the excel Spreadsheet.
Here is the code that I have right now:
import numpy as np
import pandas as pd
import xlwings as xw
from xlwings.constants import Direction
import sys
import requests
from requests_ntlm import HttpNtlmAuth
pd.options.mode.chained_assignment = None
def Upload():
wb = xw.Book.caller()
ws = wb.sheets['Sheet1']
#Read filename from excel
fileName = sys.argv[1]
#Enter SharePoint ONline site and target library
SP_URL = ws.range('C7').value
folder_URL = ws.range('C8').value
#Set up the url for requesting file upload
request_URL = SP_URL + '/_api/web/getfolderbyserverrelativeurl(\'' +
folder_URL + '\')/Files/asdd(url=\'' + fileName + '\',overwrite=true)'
#read in the file that we are going to upload
file = open(fileName, 'rb')
headers = {'Content-Type': 'application/json; odata=verbose', 'accept':
'application/json;odata=verbose'}
r = requests.post(SP_URL +
"/_api/contextinfo",auth=HttpNtlmAuth('Domain\\username','password'),
headers=headers)
formDigestValue = r.json()['d']['GetContextWebInformation']
['FormDigestValue']
headers = {'Content-Type': 'application/json; odata=verbose', 'accept':
'application/json;odata=verbose', 'x-requestdigest' : formDigestValue}
uploadResult =
requests.post(request_URL,auth=HttpNtlmAuth('Domain\\username','password'),
headers=headers, data=file.read())
I am receiving the following error:
formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']
KeyError: 'd'
requests_ntlm package
allows for HTTP NTLM authentication using the requests library
but NTLM is not supported for SharePoint Online.
Instead of requests_ntlm i would suggest to utilize Office365-REST-Python-Client (it supports to specify user credentials and consumes SharePoint REST API) package to upload file into SharePoint Online, for example:
ctx_auth = AuthenticationContext(url=settings['url'])
if ctx_auth.acquire_token_for_user(username=settings['user_credentials']['username'],
password=settings['user_credentials']['password']):
ctx = ClientContext(settings['url'], ctx_auth)
target_list = ctx.web.lists.get_by_title("Documents")
info = FileCreationInformation()
file_name = "Book.xlsx"
path = "{0}/data/{1}".format(os.path.dirname(__file__), file_name)
with open(path, 'rb') as content_file:
info.content = content = content_file.read()
info.url = file_name
info.overwrite = True
upload_file = target_list.root_folder.files.add(info)
ctx.execute_query()
formDigestValue = r.json()['d']['GetContextWebInformation']['FormDigestValue']
KeyError: 'd'
All this means is that the response content doesn't have 'd' as a key. Try looking at the json code
print(r.content) or something, there could be an error message indicating what is wrong with your post request