Hướng dẫn how do i download an excel file from sharepoint using python? - làm cách nào để tải xuống tệp excel từ sharepoint bằng python?

Tôi đang cố gắng tải các tệp Excel từ trang web SharePoint công việc của mình xuống một thư mục cục bộ bằng Python. Tôi đã viết một mã để xác thực thành công vào trang web SharePoint. Nhưng cần trợ giúp trong việc tải xuống các tệp excel từ thư viện tài liệu SharePoint. Tôi chưa quen với Python và thực sự sẽ đánh giá cao sự giúp đỡ của bạn :) Dưới đây là mã của tôi:

import urllib.request
import requests
from requests_ntlm import HttpNtlmAuth


def sharepointlogin():
    site = "https://abc.sharepoint.com/site"
    username = "*******"
    password = "*******"

    response = requests.get(site, auth=HttpNtlmAuth(username, password))
    print(response.status_code)

def filedownload():

    print('Downloading file')

    url = 'https://abc.sharepoint.com'
    urllib.request.urlretrieve(url, 'C:\Downloads\filename.xlsx')

    print("File Downloaded")

    print("Download complete")


sharepointlogin()

filedownload()

Hỏi ngày 18 tháng 12 năm 2018 lúc 8:35Dec 18, 2018 at 8:35

1

import requests

from getpass import getpass
from requests_ntlm import HttpNtlmAuth

url = "https://share.something.com/path/file.xlsx"

session = requests.Session()
session.verify = False

username = input("Enter your username: ")
password = getpass("Enter your password: ")

session.auth = HttpNtlmAuth(username, password)
response = session.get(url)

with open("output.xlsx", wb) as f:
    f.write(response.content)

Đã trả lời ngày 23 tháng 9 năm 2020 lúc 5:19Sep 23, 2020 at 5:19

Hướng dẫn how do i download an excel file from sharepoint using python? - làm cách nào để tải xuống tệp excel từ sharepoint bằng python?

HofbrhofbrHofbr

5925 Huy hiệu bạc25 Huy hiệu Đồng5 silver badges25 bronze badges

3

bài chuyển hướng

← Trước đó → Previous Next

Làm cách nào để tải xuống một tệp từ thư viện SharePoint Online bằng Python?

Các mục cần thiết để chạy tập lệnh trong ví dụ này: Office365 REST PYTHON CLIENT .microsoft.com/en-us/sharePoint/dev/solution-hướng dẫn/bảo mật-aponly-azureacs Bạn có thể tạo một nguyên tắc ứng dụng được giới hạn trong một trang web, danh sách, thư viện hoặc kết hợp chúng: https: // piyushksingh.com/2018/12/26/register-app-in-sharepoint/
Office365 Rest Python Client library:
https://pypi.org/project/Office365-REST-Python-Client/
SharePoint App Only Client Id and Secret:
Microsoft documentation:
https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
You can create an app principle that is limited to a single site, list, library, or a combination of them:
https://piyushksingh.com/2018/12/26/register-app-in-sharepoint/

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

app_settings = {
    'url': 'https://YOURtenant.sharepoint.com/sites/somesite/',
    'client_id': '12344-abcd-efgh-1234-1a2d12a21a2121a',
    'client_secret': 'Oamytacohungry234343224534543=',
}

context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])

ctx = ClientContext(app_settings['url'], context_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()

response = File.open_binary(ctx, "/Shared Documents/Invoice.pdf")
with open("./Invoice.pdf", "wb") as local_file:
    local_file.write(response.content)

Hướng dẫn how do i download an excel file from sharepoint using python? - làm cách nào để tải xuống tệp excel từ sharepoint bằng python?

Nếu tập lệnh trên không hoạt động, hãy lùi lại một bước và đảm bảo bạn được kết nối với trang web.

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File

app_settings = {
    'url': 'https://YOURtenant.sharepoint.com/sites/somesite/',
    'client_id': '12344-abcd-efgh-1234-1a2d12a21a2121a',
    'client_secret': 'Oamytacohungry234343224534543=',
}

context_auth = AuthenticationContext(url=app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])

ctx = ClientContext(app_settings['url'], context_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()

print("Site title: {0}".format(web.properties['Title']))

Xin chào, tôi đang cố gắng tải xuống tệp XLS từ các nhóm được chia sẻ các nhóm của chúng tôi. Tôi đã nhận được nó để làm việc hầu hết các cách nhưng đang có hai vấn đề. Đầu tiên, XLS đã tải xuống luôn bị hỏng và chỉ có kích thước 1kb. Thứ hai, tôi không thể tìm ra cách chỉ ra thư mục nơi XLS được sao chép. Nó hiện đang sao chép nó vào cùng một vị trí với tệp .py. Bất kỳ trợ giúp sẽ được đánh giá cao.
I am attempting to download a xls file from our Teams shared files. I have gotten it to work most of the way but am having two issues.
The first, the resulting downloaded xls is always corrupted and is only 1kb in size.
The second, I cant figure out how to dictate the directory where the xls is copied to. It currently copies it to the same location as the .py file.
Any help would be appreciated.

Từ Office365.SharePoint.Client_Context Nhập máy kháchContextFrom Office365.Runtime.Auth.Authentication_Context Nhập xác thựcContextFrom Office365.SharePoint.Files.File Nhập tệp Nhập tệp
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.files.file import File

SITE_URL = "https: // [CO của chúng tôi] .SharePoint.com/nhóm/[kênh nhóm của chúng tôi]" Thư viện = "
library = "/Shared Documents/"
filename = "test.xlsx"
pathToFile = library + filename

app_principal = {'client_id': '[id máy khách của chúng tôi]', 'client_secret': '[Bí mật khách hàng của chúng tôi]',}
'client_id': '[our client id]',
'client_secret': '[our client secret]',
}

CTX_AUTH = xác thựcContext (SITE_URL) )
ctx_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])
ctx = ClientContext(site_url, ctx_auth)
web = ctx.web
ctx.load(web)
ctx.execute_query()

filetoretrieve = file.open_binary (ctx, pathtofile) output = open (fileName, 'wb'). write (fileToreTrieve.content)
output = open(filename, 'wb').write(fileToRetrieve.content)

Làm thế nào tôi có thể truy cập vào các tài liệu SharePoint bằng Python?

Đây là mã:..
Nhập SharePy ..
từ Kết nối nhập cảnh SharePy ..
từ sharepy nhập sharepointsession ..
Nhập nhật ký ..
Tên người dùng = 'ABCD123'.
Mật khẩu = '1234'.
S = SharePy.Kết nối (Spurl, tên người dùng, mật khẩu).
# Tạo tiêu đề cho yêu cầu HTTP ..

Tôi có thể sử dụng Python với SharePoint không?

Tập lệnh Python để tự động tải lên kịch bản SharePoint Python là một cách tuyệt vời để tự động hóa nhiều quy trình mà bạn sử dụng hàng ngày như cập nhật bảng tính Excel với dữ liệu từ API, gửi báo cáo PDF hàng ngày hoặc tải tệp lên Google Cloud. Python scripting is a great way to automate many processes that you use daily such as updating an excel spreadsheet with data from an API, sending out daily PDF reports, or uploading files to a google cloud.