Commons:WriteSDoCfromExcel
writeSDoCfromExcel is a Python tool that adds Wikidata QIDs to the Structured Data on Commons (SDoC) of files listed in an Excel sheet. It supports one or multiple properties (e.g. depicts (P180) depicts, creator (P170) creator), de-duplicates safely, and logs every action.
For example, it can add putto (Q284865) to depicts (P180) for File:Atlas_Schoemaker-UTRECHT-DEEL1-3120-Utrecht,_Utrecht.jpeg based on rows in your Excel.
Latest update: 17 October 2025
What it does
- Reads your Excel (sheet & column names are configurable in the script).
- Per row and per mapped property:
** Determines the file’s M-ID (uses CommonsMid if given; otherwise resolves it from CommonsFile, which can be a full URL, a File: title, or a bare filename).
** Validates inputs (M-ID, P-id, Q-id).
** Fetches the file’s current SDoC values for that property.
** Skips duplicates (won’t add a QID that’s already present).
** Adds the new claim via the Commons API (unless running in DRY_RUN mode).
- Writes a CSV log with:
ADDED,SKIPPED_DUPLICATE,SKIPPED_INVALID,WOULD_ADD(dry-run), orERROR.
Requirements
- Python 3.10+
- Libraries:
pandas,requests,urllib3(Retry), and optionalpython-dotenvfor loading credentials.
pip install pandas requests urllib3 python-dotenv
Configuration
The tool is IDE-friendly (PyCharm / VS Code). Configure the CONFIG block inside the script and run it.
Excel columns
- CommonsFile (required)
- A Commons file identifier:
- a full Commons file URL, e.g.
https://commons.wikimedia.org/wiki/File:Example.jpg, - a
File:title, e.g.File:Example.jpg, or - a bare filename, e.g.
Example.jpg.
- a full Commons file URL, e.g.
- These are used to resolve the M-ID when needed.
- CommonsMid (optional)
- The media entity id like
M123456. If omitted or empty, the script will resolve the M-ID from CommonsFile automatically.
- QID columns (one per property)
- For each property you want to write, provide a column with QIDs (e.g.
QidDepictsfor depicts (P180)). Map them inCONFIG["PROP_MAP"]. Use one QID per row; multiple QIDs for the same file/property are handled as multiple rows.
Script CONFIG
Key settings (inside the script):
EXCEL_PATH,SHEET_NAMEFILE_COLUMN,MID_COLUMN- Property mapping via
PROP_MAP(e.g.[("P180","QidDepicts")]) or singlePROPERTY+QID_COLUMN DRY_RUN(simulate vs. write),PAUSE(seconds between edits),MAX_EDITS(safety cap),VERBOSELOG_CSV(path supports{timestamp})USER_AGENT
Credentials
Recommended: use a bot password and store credentials in a .env file next to the script. But you can also use your regular Wikimedia credentials. The script loads it automatically if CONFIG["DOTENV_PATH"] points to it. Example:
COMMONS_USER="YourWikiUsername"
COMMONS_PASS="YourWikiPassword"
COMMONS_USER_AGENT="KB-Excel-SDoC-Writer/1.0 (User:YourWikiUsername; your.email@example.org)"
You can also set CONFIG["USER"] / CONFIG["PASSWORD"] directly (not recommended). If login fails or is omitted and DRY_RUN is False, edits may be attributed to your IP.
Running
- Set
DRY_RUN: Trueto simulate and review output/logs.
- Run the script (from your IDE or shell).
- Inspect console output and the CSV in the
logs/folder.
When satisfied, set DRY_RUN: False to perform real edits. Keep a non-zero PAUSE and consider a MAX_EDITS cap.
Logging
Each attempt is logged to CSV with:
timestamp, commons_mid, commons_file, property, qid, action, details, edit_id, dry_run
Console messages include full Commons and Wikidata URLs.
Notes & etiquette
- Uses retries/backoff for transient errors and a
maxlaghint on writes. - Please be courteous to Commons: keep
PAUSE > 0, use a bot account, and test in small batches. - The script adds values of the wikibase-item datatype (QIDs). Other datatypes need code changes.
Known issues
- If your Excel has empty
CommonsFilecells and no validCommonsMid, those rows are skipped as invalid. - File title normalization handles URLs,
File:titles and bare filenames; odd encodings may still need cleanup.
Source
- Repository on Github: writeSDoCfromExcel
- Python script: WriteSDoCfromExcel.py
Contact & credits
- Written by Olaf Janssen (KB, National Library of the Netherlands).
- Contact Olaf via his KB expert page or Wikimedia user page.
- Many thanks to User:Multichill and the Wikimedia Commons community.
License
Released under CC0 (public domain). Feel free to reuse and adapt. Attribution (KB, National Library of the Netherlands) is appreciated but not required.
Category:Structured Data on Commons Category:WriteSDoCfromExcel