Automatically updating/replacing/editing a mediawiki page from a python script with mwclient – an example

Agile, Media wiki, Project management

Many workplaces have mediawiki wikis – and these are often the information hub for a team or, indeed, the entire organisation.

Rather than generating  a new web page on a different web server to display a semi-regularly updated computer generated report, I thought (perhaps) it could be convenient to have a tool which permitted the easy replacement of a particular page.

I recently undertook to develop such a tool – and, in researching, discovered the excellent mwclient library   – although it _is_ somewhat lacking in examples/documentation.

As luck had it, the reporting tool I was using already generated output in mediawiki markup  on stdout – so all I needed to do was write a tool that could read a page contents from stdin and then upload it to a predefined page on wiki.

Here’s what I came up with:

#! /usr/bin/env python

import mwclient
import sys

def replace_page(wikiname,
    site = mwclient.Site(wikiname,
    site.login(username, password)
    page = site.Pages[pagename]
    some = page.edit(), summary='Automatic update')

def main():
    wikibasepath="/wiki/"  # This assumes that your wiki pages all reside under
# Usage cat somefile.txt |
if __name__ == "__main__":

Beware: it’s not best practice to hard-wire passwords in plain text in your own code!

Obviously, it would be childsplay to write a more general purpose tool that read the the various parameters in from a config file or as command line parameters (perhaps using getpass to securely capture the password) – but this was not needed in my particular environment and so is is left as an exercise for the reader.


8 thoughts on “Automatically updating/replacing/editing a mediawiki page from a python script with mwclient – an example

  1. Seems nice, but I don’t understand how to use it ? Put the script in a Python file and then how to execute it ? > mediwikicontent.txt ? By the way, how is the Main Page name pagename=’User:Main Page’ ?

  2. Yves: In the example “pagename” simply matched the wasy mediawiki had been deployed at my former employer. The setup of your mediawiki may be quite different.

    How to use the script:

    Assuming that:

    1) you’ve created a file “” containing the above code along with changes to wikiname, wikibasepath, pagename, username and password to match those required by your wiki. “pagename” is simply the mediawiki page you want your file to appear on – and, in our wiki, it just so happened that each user had their own page
    2) you have some text in a file named “somefile.txt”

    you simply need to pipe the file (or some other program that produces text output) into the script:

    On *nix systems enter the following at the command prompt:

    cat somefile.txt |

    On windows systems enter the following at the command prompt:

    type somefile.txt |

    Hope this helps!

  3. Hi…

    First Thanks for this code. Its very crisp and helpful.

    I tried the same code with a simple wiki page (
    The error I am getting is
    “ValueError: No JSON object could be decoded”

    When I tried to print the object which is trying to be decoded, the content in a html format.
    I do not how to get along with this.
    Kindly help me out.


  4. Sowmya: I’d be cautious about running this against a public resource like like – really it’s best used for private wikis!

    It sounds like you may be trying to run it against a page you don’t have permission to edit. Try running it again against a page on your private wiki and see if there are any error messages embedded in the html returned which might shed some light on what went wrong – you can share the output on and I’ll take a look if you like?

  5. Thanks for the code. I got the following error:
    mwclient.errors.MediaWikiVersionError: Requires version 1.16, current version is 1.15

    Can you please help me how can I resolve this.

    Thanks in Advance.

    1. Hi Mohan,

      I’ve not encountered this issue before myself (and no longer use this code in my current role) so it’s hard to investigate.

      However, it looks like the 3rd party open source library used by the example code (mwclient) which originates from here: is not compatible with your wiki version as the version you are using is too old.

      If you can upgrade your MediaWiki server to a newer version (1.16 or later – current version seems to be 1.25, available here ), that may well fix the issue -although it’s hard to be completely sure without trying.

      Hope this helps; best of luck!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s