skip to navigation
skip to content

mwtemplates 0.3.0

MediaWiki template parser and editor

Latest Version: 0.4.0

mwtemplates is a simple MediaWiki wikitext template parser and editor, based on a Python rewrite of the MediaWiki preprocessorDOM.php. Tested with python 2.6, 2.7, 3.2, 3.3 and 3.4.


The package is on PyPI, so you can install it using pip, easy_install or similar:

$ pip install mwtemplates

Running tests

To run tests, clone the repo and do:

$ pip install -r requirements.txt
$ pip install -r test_requirements.txt
$ py.test -x tests --pep8 mwtemplates -v --cov mwtemplates --doctest-modules

Usage examples

Editing a template:

from mwtemplates import TemplateEditor
txt = u"""
{{Infoboks geografi
| kart = Svalbard_kart1.png
| land = Norge
| status = Øygruppe
| administrasjon = [[Odd Olsen Ingerø]] ''<small>(2009)</small>''
| administrasjonsnavn = [[Sysselmannen på Svalbard|Sysselmann]]
| areal = 61022
| befolkning = 2527
| befolkningsår = [[2011]]
| url =
te = TemplateEditor(txt)
te.templates['infoboks geografi'][0].parameters['land'] = 'Russland'
print te.wikitext()

Updating a page on Wikipedia using mwclient

from mwclient import Site
from mwtemplates import TemplateEditor

site = Site('')
site.login('USERNAME', 'PASSWORD')
page = site.pages['SOME_PAGE']
te = TemplateEditor(page.edit())
if 'SOME_TEMPLATE' in page.templates:
   tpl = te.templates['SOME_TEMPLATE'][0]
   tpl.parameters['test'] = 'Hello', summary='...')

Removing a template argument:

from mwtemplates import TemplateEditor
te = TemplateEditor(u"Hello {{mytpl | a=2 | b=3 | c=4 }} world")

Removing the first instance of a template:

from mwtemplates import TemplateEditor
te = TemplateEditor(u"Hello {{mytpl}} world {{mytpl}}")
File Type Py Version Uploaded on Size
mwtemplates-0.3.0.tar.gz (md5) Source 2014-08-02 10KB