Script Plone from Python#
At the end of this path, you will have a Python script that keeps content on a Plone site in step with a data file, previews its changes, and handles errors.
Try it now#
This script reads demo.plone.org, a public site that resets every night, without logging in.
from plone_client import PloneClient
with PloneClient("https://demo.plone.org") as site:
for item in site.search.iter(portal_type="Document", limit=3):
print(item["title"])
uv run --with plone-client demo.py
You need#
Python 3.11 or later, and uv.
Docker, from the second step on.
No Plone knowledge: Get started has what you need.
Follow these steps#
Install plone-client adds plone-client to your project, because the rest of the path needs it.
Your first script is the core of the path: you read a site, change it, and end with a sync from a JSON file.
Content model shows what your script reads and writes, so that your fields and blocks land where you expect.
Handle errors turns the failures of a real site into code paths.
Sync content from an external source takes the tutorial’s script to production.
Migrate content between two sites applies the same ideas to two sites.
Going further#
Services has an example for every service.
Block helpers builds the blocks of a page in Python.
Exceptions lists the exception for each kind of failure.
Work with large sites keeps scripts fast on large sites.
When to call the API directly says when to step outside the managers.
If something breaks#
Troubleshoot goes from the symptom to the fix.
Before you connect a real site#
Log in as a dedicated Plone user with only the permissions the script needs, not
admin.Start with
read_only=True, which refuses every change before sending it.Pass
dry_run=Trueon the first run that writes, and readclient.dry_run_log; see Preview changes and protect a site.Keep the password out of the script: use a profile, or an environment variable with
from_env(); see Use the command line in scripts and CI.Catch the exceptions of Exceptions, so that a refused change stops the script instead of half-applying it.