Skip to content

Get html source of sharepoint page? #491

Closed

Description

@WG-

How can I easily fetch the html source code from a sharepoint page?

The snippet below works but I do not manage to get the plain html content?

from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
site_url = "https://company.sharepoint.com/sites/<some site>"
ctx = ClientContext(site_url).with_credentials(UserCredential("[email protected]", "somepassword"))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(web.properties)
print(ctx.web.get().execute_query())

Activity

  1. henrikruneskog commented on Jul 7, 2023

    @henrikruneskog

    I also have this question. @WG- did you solve it?

  2. office365-maintainer commented on Oct 3, 2026

    @office365-maintainer

    This is now supported through the modern Site Pages API. Your snippet only loads the web's own properties — to get a page's HTML you read the page's canvas content (CanvasContent1):

    from office365.sharepoint.client_context import ClientContext
    
    ctx = ClientContext("https://company.sharepoint.com/sites/<site>").with_credentials(
        UserCredential("[email protected]", "password")
    )
    
    # a single page, by file name
    page = ctx.site_pages.pages.get_by_name("Home.aspx").execute_query()
    html = page.canvas_content or ""          # raw HTML stored in CanvasContent1
    
    # or every page
    for page in ctx.site_pages.pages.get().execute_query():
        print(page.file_name, len(page.canvas_content or ""), "chars")

    canvas_content is the authored HTML of the page. Embedded files, images and videos are referenced, not inlined.

    A full runnable example that exports every modern page to a .html file is in the repo: examples/sharepoint/pages/export_html.py (see also get_content.py, paged.py, and the guide at examples/sharepoint/pages/README.md).

    For classic (wiki / web part) pages, read the page file directly, e.g.:

    file = ctx.web.get_file_by_server_relative_url("/sites/<site>/SitePages/Home.aspx")
    html = file.read()

    Closing as answered — please reopen if this doesn't cover your case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requestedsharepointSharePoint REST v1 API, ClientContext

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions