Bikarhêner:Şêr/bilêvkirinjor.py

import pywikibot

site = pywikibot.Site("ku", "wiktionary")

page_title = "testpage"

page = pywikibot.Page(site, page_title)

sections = {}
current_section = None
content = ""
for line in page.text.split("\n"):
    if line.startswith("=="):
        if current_section:
            sections[current_section] = content.strip()
            content = ""
        current_section = line.strip()
    else:
        content += line + "\n"
sections[current_section] = content.strip()

new_order = [
    "== {{ziman|en}} ==",
    "=== Bilêvkirin ===",
    "=== Lêker  ==="
]

new_text = ""
for section in new_order:
    if section in sections:
        new_text += section + "\n"
        new_text += sections[section] + "\n"
        if section == "=== Bilêvkirin ===":
            new_text += "\n" 
page.text = new_text

page.save("Botê Bilêvkirin xistin bin == {{ziman|en}} ==", botflag=True)