{"id":15023169,"url":"https://github.com/danmichaelo/mwtemplates","last_synced_at":"2025-10-28T15:20:58.622Z","repository":{"id":3761770,"uuid":"4837959","full_name":"danmichaelo/mwtemplates","owner":"danmichaelo","description":"MediaWiki template parser and editor","archived":false,"fork":false,"pushed_at":"2019-09-12T03:11:52.000Z","size":136,"stargazers_count":9,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-20T14:02:22.064Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danmichaelo.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-29T22:40:55.000Z","updated_at":"2021-05-07T16:32:21.000Z","dependencies_parsed_at":"2022-09-26T17:21:23.656Z","dependency_job_id":null,"html_url":"https://github.com/danmichaelo/mwtemplates","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"purl":"pkg:github/danmichaelo/mwtemplates","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danmichaelo%2Fmwtemplates","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danmichaelo%2Fmwtemplates/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danmichaelo%2Fmwtemplates/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danmichaelo%2Fmwtemplates/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danmichaelo","download_url":"https://codeload.github.com/danmichaelo/mwtemplates/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danmichaelo%2Fmwtemplates/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261715447,"owners_count":23198762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T19:58:47.634Z","updated_at":"2025-10-28T15:20:58.534Z","avatar_url":"https://github.com/danmichaelo.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"mwtemplates\n==================\n\n.. image:: http://img.shields.io/travis/danmichaelo/mwtemplates.svg?style=flat\n   :target: https://travis-ci.org/danmichaelo/mwtemplates\n   :alt: Build status\n\n.. image:: http://img.shields.io/coveralls/danmichaelo/mwtemplates.svg?style=flat\n   :target: https://coveralls.io/r/danmichaelo/mwtemplates\n   :alt: Test coverage\n\n.. image:: https://img.shields.io/pypi/v/mwtemplates.svg?style=flat\n   :target: https://pypi.python.org/pypi/mwtemplates/\n   :alt: Latest version\n\n.. image:: https://img.shields.io/pypi/pyversions/mwtemplates.svg?style=flat\n   :target: https://pypi.python.org/pypi/mwtemplates/\n   :alt: Supported Python versions\n\n.. image:: https://img.shields.io/pypi/dm/mwtemplates.svg?style=flat\n   :target: https://pypi.python.org/pypi/mwtemplates/\n   :alt: Downloads\n\nmwtemplates is a MediaWiki wikitext template parser and editor, based on a Python rewrite of the MediaWiki preprocessorDOM.php.\nTested with python 2.7, 3.3, 3.4, 3.5\n\n\nInstallation\n-------------------\n\nThe package is on `PyPI \u003chttps://pypi.python.org/pypi/mwtemplates\u003e`_, so you can install it\nusing `pip`, `easy_install` or similar:\n\n.. code-block:: console\n\n   $ pip install -U mwtemplates\n\nOr you can grab the latest zip from `releases \u003chttps://github.com/danmichaelo/mwtemplates/releases\u003e`_.\n\nIntroduction\n------------\n\nLet's start by importing `TemplateEditor` and giving it some wikitext to eat:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e from mwtemplates import TemplateEditor\n    \u003e\u003e\u003e txt = u\"\"\"{{Infobox cheese\n    ... | name = Mozzarella\n    ... | protein = 7\n    ... }}\n    ... Mozzarella is a cheese…{{tr}}\"\"\"\n    \u003e\u003e\u003e te = TemplateEditor(txt)\n\nFirst, we can see what templates the editor found in the text:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e te.templates\n    [\u003cTemplate:\"Infobox cheese\" at line 2\u003e, \u003cTemplate:\"Tr\" at line 6\u003e]\n\nEach template is an instance of a `Template` class. Also notice that template names are normalized by upper-casing the first character. Now, we can try investigating the `Infobox cheese` template:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e te.templates['Infobox cheese']\n    [\u003cTemplate:\"Infobox cheese\" at line 2\u003e]\n\nSince there can be several instances of the same template, an array is always returned, and so we need to ask for `te.templates['Infobox cheese'][0]` to get the actual `Template`. To get the parameters:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e te.templates['Infobox cheese'][0].parameters\n    \u003cParameters: name=\"Mozzarella\", protein=\"10\"\u003e\n\nLet's say we want to change the value of the `protein` parameter from 10 to 7. We then use\nthe `wikitext()` method to return our new wikitext:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e te.templates['Infobox cheese'][0].parameters['protein'] = 7\n    \u003e\u003e\u003e print te.wikitext()\n    {{Infobox cheese\n    | name = Mozzarella\n    | protein = 10\n    }}\n    Mozzarella is a cheese…{{tr}}\n\nNotice that formatting is preserved. We could now go and add a new parameter like so:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e te.templates['Infobox cheese'][0].parameters['fat'] = 25\n    \u003e\u003e\u003e print te.wikitext()\n    {{Infobox cheese\n    | name = Mozzarella\n    | protein = 7\n    | fat = 25\n    }}\n    Mozzarella is a cheese…{{tr}}\n\nTo remove a template argument:\n\n.. code-block:: python\n\n    from mwtemplates import TemplateEditor\n    te = TemplateEditor(u\"Hello {{mytpl | a=2 | b=3 | c=4 }} world\")\n    te.templates['mytpl'].parameters.remove('b')\n\nTo remove the first instance of a template:\n\n.. code-block:: python\n\n    from mwtemplates import TemplateEditor\n    te = TemplateEditor(u\"Hello {{mytpl}} world {{mytpl}}\")\n    te.templates['mytpl'][0].remove()\n\n\nKnown issues\n----------------------------------------------\n\nThe parser doesn't handle empty `\u003cnowiki/\u003e` tags. It will raise a\n`mwtemplates.NowikiError` error if feeded a page having one, so it's\na good idea to handle those:\n\n.. code-block:: python\n\n    \u003e\u003e\u003e from mwtemplates import TemplateEditor, NowikiError\n    \u003e\u003e\u003e try:\n    \u003e\u003e\u003e     te = TemplateEditor(txt)\n    \u003e\u003e\u003e except NowikiError:\n    \u003e\u003e\u003e     print('Page contains \u003cnowiki/\u003e tags, skipping.')\n\n\nUsage with mwclient to edit pages on Wikipedia\n----------------------------------------------\n\nUpdating a page on Wikipedia using `mwclient \u003chttps://github.com/mwclient/mwclient\u003e`_\n\n.. code-block:: python\n\n   from mwclient import Site\n   from mwtemplates import TemplateEditor\n\n   site = Site('en.wikipedia.org')\n   site.login('USERNAME', 'PASSWORD')\n   page = site.pages['SOME_PAGE']\n   te = TemplateEditor(page.text())\n   if 'SOME_TEMPLATE' in page.templates:\n      tpl = te.templates['SOME_TEMPLATE'][0]\n      tpl.parameters['test'] = 'Hello'\n   page.save(te.wikitext(), summary='...')\n\n\nContributing\n------------\n\nPull requests are very welcome. Please run tests before submitting:\n\n.. code-block:: console\n\n    $ python setup.py test\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanmichaelo%2Fmwtemplates","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanmichaelo%2Fmwtemplates","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanmichaelo%2Fmwtemplates/lists"}