杰瑞科技汇

如何用Python连接SharePoint?

Of course! Working with SharePoint using Python is a common task for automating document management, data retrieval, and business process workflows. The most popular and robust library for this is SharePointPnP.PowerShell.

如何用Python连接SharePoint?-图1
(图片来源网络,侵删)

This guide will cover:

  1. Core Concepts: Understanding how SharePoint APIs work.
  2. The Best Python Library: SharePointPnP.PowerShell and its wrapper pnp.
  3. Prerequisites: What you need before you start.
  4. Authentication: Connecting to SharePoint securely.
  5. Code Examples: Common tasks like listing sites, uploading files, and reading list data.
  6. Advanced Scenarios: Using Microsoft Graph API.

Core Concepts: SharePoint REST API

Under the hood, almost all modern SharePoint interactions happen through the SharePoint REST API. This is a standard RESTful web service that you can call using HTTP requests. It allows you to:

  • Read and write list items (like rows in a database).
  • Upload and download files from document libraries.
  • Manage sites, users, and permissions.
  • Query search results.

Python libraries act as convenient wrappers around this API, saving you from writing raw HTTP requests.


The Best Python Library: pnp

The modern, recommended way to interact with SharePoint from Python is by using the pnp library. This is the official Python client library for the PnP (Patterns and Practices) framework, which provides a rich set of features and a clean, Pythonic API.

如何用Python连接SharePoint?-图2
(图片来源网络,侵删)

It's important to distinguish it from the SharePointPnP.PowerShell module, which is for PowerShell. The pnp Python library is the direct equivalent.

Key Features:

  • Simple, intuitive syntax.
  • Handles authentication for you.
  • Provides high-level methods for common operations (e.g., get_files(), get_list_items()).
  • Supports both SharePoint REST and the newer Microsoft Graph API.

Prerequisites

Before you write any code, you need:

  1. Python 3.6+: Ensure you have Python installed.
  2. SharePoint Site URL: The URL of the SharePoint site you want to connect to (e.g., https://yourtenant.sharepoint.com/sites/YourSite).
  3. Permissions: An account with access to the SharePoint site. It's highly recommended to use an App Registration for automation instead of a user's credentials.
  4. Install the Library: Open your terminal or command prompt and install the pnp library.
    pip install pnp

Authentication (The Most Important Part)

There are two primary methods for authenticating. Method 1 (App-only) is strongly recommended for automation.

如何用Python连接SharePoint?-图3
(图片来源网络,侵删)

Method 1: App-Only Authentication (Recommended for Automation)

This method uses a "service principal" (an App Registration in Azure AD) to access SharePoint without a user's password. It's more secure and doesn't expire.

Steps to Set Up:

  1. Register an App in Azure AD:
    • Go to the Azure Portal -> Azure Active Directory -> App registrations.
    • Click New registration.
    • Give it a name (e.g., PythonSharePointAutomation).
    • Choose "Accounts in this organizational directory only".
    • Click Register.
  2. Get Credentials:
    • Copy the Application (client) ID and Directory (tenant) ID.
    • Go to Certificates & secrets -> New client secret.
    • Add a description and choose an expiry, then click Add.
    • Immediately copy the secret's Value. You will not see it again.
  3. Grant API Permissions:
    • Go to API permissions -> Add a permission -> SharePoint.
    • Select Application permissions.
    • Choose Sites.FullControl.All (full control to all site collections) or a more specific permission like Sites.ReadWrite.All.
    • Click Add permissions.
    • Click the Grant admin consent for [Your Tenant] button. This step is crucial.
  4. Get Site ID:
    • Navigate to your target SharePoint site in a browser.
    • Replace the URL with /_api/site/id and press Enter. You'll get a JSON response with the site's ID. Copy this ID (it looks like ).

Python Code for App-Only Authentication:

from pnp import SharePoint, AuthenticationType
# Your credentials from Azure AD and SharePoint
client_id = "YOUR_APPLICATION_CLIENT_ID"
tenant_id = "YOUR_TENANT_ID"
client_secret = "YOUR_CLIENT_SECRET_VALUE"
site_url = "https://yourtenant.sharepoint.com/sites/YourSite"
site_id = "YOUR_SITE_ID" # e.g., "00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000,00000000-0000-0000-0000-000000000000"
# Create the SharePoint client
sp = SharePoint(
    site_url=site_url,
    auth_type=AuthenticationType.APP_ONLY,
    client_id=client_id,
    tenant_id=tenant_id,
    client_secret=client_secret,
    site_id=site_id
)
# Now you can use the 'sp' object to interact with SharePoint

Method 2: User Credentials (Interactive or Non-Interactive)

This method uses a username and password. It's simpler to set up but less secure for long-term automation. It can also be blocked by Multi-Factor Authentication (MFA).

from pnp import SharePoint, AuthenticationType
# Your user credentials
username = "user@yourtenant.com"
password = "your_password" # Be careful with hardcoding passwords!
site_url = "https://yourtenant.sharepoint.com/sites/YourSite"
# Create the SharePoint client
sp = SharePoint(
    site_url=site_url,
    auth_type=AuthenticationType.USER_CREDENTIALS,
    username=username,
    password=password
)

Code Examples

Once you have your sp client object, here are some common tasks.

Example 1: List All Files in a Document Library

from pnp import SharePoint, AuthenticationType
# Use the authentication setup from above to create the 'sp' client
# ...
try:
    # List files in the "Documents" library
    documents_library = sp.web.lists.get_by_title("Documents")
    files = documents_library.root_folder.get_files()
    print(f"Found {len(files)} files in the Documents library:")
    for file in files:
        print(f"- {file.name} (Size: {file.length} bytes)")
except Exception as e:
    print(f"An error occurred: {e}")

Example 2: Upload a File to a Document Library

from pnp import SharePoint, AuthenticationType
import os
# ...
file_path = "local_report.pdf" # Path to the file on your local machine
library_name = "Documents"
file_name = os.path.basename(file_path)
try:
    # Open the file in binary mode
    with open(file_path, "rb") as file_content:
        # Get the target folder in the library
        target_folder = sp.web.lists.get_by_title(library_name).root_folder
        # Upload the file
        uploaded_file = target_folder.upload_file(
            file_name=file_name,
            file_content=file_content.read()
        )
    print(f"Successfully uploaded '{file_name}' to '{library_name}'.")
    print(f"Server-relative URL: {uploaded_file.server_relative_url}")
except Exception as e:
    print(f"An error occurred: {e}")

Example 3: Read and Create List Items

Let's say you have a custom list named "Projects" with columns "Title" and "Status".

from pnp import SharePoint, AuthenticationType
# ...
list_name = "Projects"
try:
    # --- READ ITEMS ---
    projects_list = sp.web.lists.get_by_title(list_name)
    items = projects_list.get_items()
    print(f"--- Items in '{list_name}' list ---")
    for item in items:
        # Access field values using their internal names (usually Title, Status, etc.)
        print(f"- ID: {item.id}, Title: {item['Title']}, Status: {item['Status']}")
    # --- CREATE A NEW ITEM ---
    print("\n--- Creating a new project ---")
    new_item_payload = {
        "Title": "Website Redesign",
        "Status": "In Progress"
    }
    # Use the .create() method of the list object
    created_item = projects_list.create_item(new_item_payload)
    print(f"Successfully created new item with ID: {created_item.id}")
except Exception as e:
    print(f"An error occurred: {e}")

Advanced Scenarios: Using Microsoft Graph API

For some operations, especially those related to user profiles, groups, and mail, the Microsoft Graph API is the preferred modern API. The pnp library also supports this.

You'll need to grant Microsoft Graph permissions in your Azure AD App Registration (e.g., User.Read.All, Sites.ReadWrite.All).

from pnp import SharePoint, AuthenticationType, Graph
# Use the same app-only credentials as before
client_id = "YOUR_APPLICATION_CLIENT_ID"
tenant_id = "YOUR_TENANT_ID"
client_secret = "YOUR_CLIENT_SECRET_VALUE"
# Create a Graph client
graph = Graph(
    auth_type=AuthenticationType.APP_ONLY,
    client_id=client_id,
    tenant_id=tenant_id,
    client_secret=client_secret
)
try:
    # Get all users in the tenant
    users = graph.users.get()
    print(f"Found {users['@odata.count']} users.")
    for user in users['value']:
        print(f"- {user['displayName']} ({user['mail']})")
except Exception as e:
    print(f"An error occurred: {e}")

Summary and Best Practices

Task Recommended Library Authentication Method
General SharePoint Automation pnp App-Only (Azure AD App Registration)
Quick & Simple Script pnp User Credentials (if MFA is off)
User/Group Management pnp (with Graph) App-Only
Legacy Systems requests (direct REST calls) App-Only or User Credentials

Best Practices:

  1. Never hardcode secrets. Use environment variables or a secrets management tool (like python-dotenv for development).
  2. Always use App-Only authentication for any automation or script that runs unattended.
  3. Handle exceptions. Network issues and permission errors are common. Wrap your SharePoint calls in try...except blocks.
  4. Check permissions. If your script fails, the first thing to check is whether your App Registration has the correct API permissions granted with admin consent.
分享:
扫描分享到社交APP
上一篇
下一篇