问题描述
快速一.
我的 XLSX 文件位于共享点驱动器上,无法在 python 中使用 openpyxl 打开它,如果它存储在我的本地驱动器上,它运行良好.
I have XLSX file located on sharepoint drive and cannot open it using openpyxl in python, it works well if it is stored on my local drive.
我试过了.
from openpyxl import load_workbook
wb = load_workbook('https://content.potatocompany.com/workspaces/PotatoTeam/Shared Documents/XYZ errors/XYZ Errors_Confirm.xlsx')
抛出这个异常:
C:\Anaconda\lib\site-packages\openpyxl\reader\excel.py in load_workbook(filename, use_iterators, keep_vba, guess_types, data_only)
123 except (BadZipfile, RuntimeError, IOError, ValueError):
124 e = exc_info()[1]
--> 125 raise InvalidFileException(unicode(e))
126 wb = Workbook(guess_types=guess_types, data_only=data_only)
127
InvalidFileException: [Errno 22] invalid mode ('rb') or filename: 'https://...
我错过了什么吗?我需要在 python 中阅读其中一张工作表的内容.
Am I missing something?I need to read the content of one of the sheets in python.
根据 crussell 的建议,我收到 401 UNAUTHORIZED:
Using crussell's advice, I receive 401 UNAUTHORIZED:
import requests
import urllib
from openpyxl import load_workbook
from requests.auth import HTTPBasicAuth
file = "https://content.potatocompany.com/workspaces/PotatoTeam/Shared Documents/XYZ errors/XYZ Errors_Confirm.xlsx"
username = 'PotatoUser'
password = 'PotatoPassword'
resp=requests.get(file, auth=HTTPBasicAuth(username, password))
print(resp.content)
好像sharepoint和requests不兼容,无论是摘要认证还是基本认证http://docs.python-requests.org/en/latest/user/身份验证/
Seems like sharepoint and requests are not compatible, with both Digest Authentication and Basic Authenticationhttp://docs.python-requests.org/en/latest/user/authentication/
推荐答案
不要尝试直接从网址加载,而是尝试使用 urllib.
Instead of trying to load directly from a web-address, try using urllib.
import urllib
file = "https://content.potatocompany.com/workspaces/PotatoTeam/Shared Documents/XYZ errors/XYZ Errors_Confirm.xlsx"
urllib.urlretrieve(file,"test.xlsx")
根据进一步的研究,urllib 显然被 requests.试试这个:
From further research, urllib is apparently eschewed by requests.Try this:
import requests
from requests.auth import HTTPBasicAuth
file = "https://content.potatocompany.com/workspaces/PotatoTeam/Shared Documents/XYZ errors/XYZ Errors_Confirm.xlsx"
username = 'myUsername'
password = 'myPassword'
resp=requests.get(file, auth=HTTPBasicAuth(username, password))
output = open('test.xlsx', 'wb')
output.write(resp.content)
output.close()
要安装请求:
pip install requests
这篇关于使用python中的openpyxl读取存储在共享点位置的xlsx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!