質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

6457閲覧

SharePointにあるエクセルファイルをO365ライブラリを用いてデータフレームに読み込む方法

angew

総合スコア20

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2022/03/31 01:58

編集2022/04/05 15:17

SharePointにあるエクセルファイルをO365ライブラリを用いてデータフレームに変換したいのです。
認証は問題なく行えています。
responseにはhtmlコードが返ってきます。
その中から必要なExcelのデータを抜き出す必要があるはずなのですが、どこを抜き出せばよいのかわかりません。
何かヒントがありましたら、教えて下さい。

<!DOCTYPE html> <html lang="ja-jp" dir="ltr" class="ms-isBot"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no" /> <meta name="robots" content="noindex" /> <script type="text/javascript"> var wopiDiagClient = { docFirstFlushTime : new Date().getTime() } ; var _wopiContextJson ={"HostName":"SharePoint ・・・・・・

python

1from office365.runtime.auth.authentication_context import AuthenticationContext 2from office365.sharepoint.client_context import ClientContext 3from office365.sharepoint.files.file import File 4import io 5import pandas as pd 6import json 7 8url="https://*****.sharepoint.com/:x:/r/sites/PEB/Shared%20Documents/********.xlsx?d=w7a40fdfaa2404********cf00059&csf=1&web=1&e=****" 9username = '*********@************' 10password = '*******' 11 12ctx_auth = AuthenticationContext(url) 13if ctx_auth.acquire_token_for_user(username, password): 14 ctx = ClientContext(url, ctx_auth) 15 web = ctx.web 16 ctx.load(web) 17 ctx.execute_query() 18 print("Authentication successful") 19 20response = File.open_binary(ctx, url) 21 22bytes_file_obj = io.BytesIO() 23bytes_file_obj.write(response.content) 24bytes_file_obj.seek(0) 25 26print(bytes_file_obj.read()) 27 28df = pd.read_excel(bytes_file_obj, sheet_name = None) 29display(df) 30
Authentication successful --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-58-46c79dfd553a> in <module> 26 print(bytes_file_obj.read()) 27 ---> 28 df = pd.read_excel(bytes_file_obj, sheet_name = None) 29 display(df) D:\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs) 297 ) 298 warnings.warn(msg, FutureWarning, stacklevel=stacklevel) --> 299 return func(*args, **kwargs) 300 301 return wrapper D:\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, thousands, comment, skipfooter, convert_float, mangle_dupe_cols, storage_options) 334 if not isinstance(io, ExcelFile): 335 should_close = True --> 336 io = ExcelFile(io, storage_options=storage_options, engine=engine) 337 elif engine and engine != io.engine: 338 raise ValueError( D:\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in __init__(self, path_or_buffer, engine, storage_options) 1069 ext = "xls" 1070 else: -> 1071 ext = inspect_excel_format( 1072 content=path_or_buffer, storage_options=storage_options 1073 ) D:\Anaconda3\lib\site-packages\pandas\io\excel\_base.py in inspect_excel_format(path, content, storage_options) 963 return "xls" 964 elif not peek.startswith(ZIP_SIGNATURE): --> 965 raise ValueError("File is not a recognized excel file") 966 967 # ZipFile typing is overly-strict ValueError: File is not a recognized excel file

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。
sharepyライブラリを用いることでできました。
参考にしたサイト↓
https://stackoverflow.com/questions/45648491/read-csv-from-sharepoint-with-pandas-python-authentication-code

投稿2022/04/05 06:17

angew

総合スコア20

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問