Python Script to Bulk Update DHIS2 User Interface Language Settings

:megaphone: Introduction

When importing large batches of users into DHIS2, we’ve observed that default accounts often have the interface set to English. To automatically switch the UI language to French for multiple users, we developed a simple Python script using the DHIS2 API.

This guide outlines the step-by-step approach and includes the full code. It’s easily customizable for other languages or DHIS2 setups.

:rocket: Tutorial

:bullseye: Goal

Automatically set the UI language of multiple DHIS2 users to French using their login credentials.

:clipboard: Requirements

  • Access to a DHIS2 instance
  • Username and password for each target account
  • Python installed locally
  • The requests library (pip install requests)
  • A CSV file listing the users

:file_folder: CSV example

csv

username,password
user1,pass1
user2,pass2
user3,pass3

Suggested filename: users.csv

:snake: Python Script

python

import requests
import csv

base_url = "https://your-dhis2-instance.org"

with open("users.csv", newline='') as file:
    reader = csv.DictReader(file)
    for row in reader:
        username = row["username"]
        password = row["password"]

        session = requests.Session()
        session.auth = (username, password)

        url = f"{base_url}/api/userSettings/keyUiLocale"
        headers = {"Content-Type": "text/plain"}

        response = session.post(url, data="fr", headers=headers)

        if response.status_code in [200, 201]:
            print(f"{username} ✅ Language set to French")
        else:
            print(f"{username} ❌ Error {response.status_code} : {response.text}")

:play_button: Execution

bash

python changer_langue.py

:white_check_mark: Expected output

For each user, the terminal shows success or error status.

:graduation_cap: Conclusion

This script helped us automate a repetitive administrative task and improve user experience. Feel free to test it, modify it for your setup, or share improvements.

I’m happy to answer questions or collaborate if you’d like to adapt it for other languages or scenarios :raising_hands:

2 Likes