forked from O365/python-o365
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorageDownloadFile
More file actions
executable file
·46 lines (34 loc) · 1.4 KB
/
storageDownloadFile
File metadata and controls
executable file
·46 lines (34 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python3
# To generate an Office365 token:
# python3
# from O365 import Account
# account = Account(credentials=('yourregisteredappname', 'yoursecret'))
# account.authenticate(scopes=['files.read', 'user.read', 'offline_access'])
# It will return a URL, go to this in a browser, accept the permissions, then paste in the URL you are redirected to
# YOU MAY HAVE TO SWITCH TO THE 'OLD' VIEW TO DO THIS!
import pandas as pd
from O365 import Account
# Generated on the app registration portal
registered_app_name='yourregisteredappname'
registered_app_secret='yoursecret'
# File to download, and location to download to
dl_path='/path/to/download'
f_name='myfile.xlsx'
print("Connecting to O365")
account = Account(credentials=(registered_app_name, registered_app_secret), scopes=['files.read', 'user.read', 'offline_access'])
storage = account.storage() # here we get the storage instance that handles all the storage options.
# get the default drive
my_drive = storage.get_default_drive()
print(f"Searching for {f_name}...")
files = my_drive.search(f_name, limit=1)
if files:
numberDoc = files[0]
print("... copying to local machine")
operation = numberDoc.download(to_path=dl_path)
else:
print("File not found!")
exit()
print("Reading sheet to dataframe")
df = pd.read_excel(f'{dl_path}/{f_name}')
with pd.option_context('display.max_rows', None, 'display.max_columns', None):
print(df)