wikitextparser 3.0.0


pip install wikitextparser

  Latest version

Released: Oct 08, 2026

Project Links

Meta
Author: 5j9
Requires Python: >=3.9

Classifiers

Programming Language
  • Python

License
  • OSI Approved :: GNU General Public License v3 (GPLv3)

Topic
  • Text Processing
https://github.com/5j9/wikitextparser/actions/workflows/tests.yml/badge.svg https://codecov.io/github/5j9/wikitextparser/coverage.svg?branch=master https://readthedocs.org/projects/wikitextparser/badge/?version=latest

WikiTextParser

A simple to use WikiText parsing library for MediaWiki.

The purpose is to allow users easily extract and/or manipulate templates, template parameters, parser functions, tables, external links, wikilinks, lists, etc. found in wikitexts.

Installation

  • Python 3.9+ is required

  • pip install wikitextparser

Usage

>>> import wikitextparser as wtp

WikiTextParser can detect sections, parser functions, templates, wiki links, external links, arguments, tables, wiki lists, and comments in your wikitext. The following sections are a quick overview of some of these functionalities.

You may also want to have a look at the test modules for more examples and probable pitfalls (expected failures).

Templates

>>> parsed = wtp.parse("{{text|value1{{text|value2}}}}")
>>> parsed.templates
[Template('{{text|value1{{text|value2}}}}'), Template('{{text|value2}}')]
>>> parsed.templates[0].arguments
[Argument("|value1{{text|value2}}")]
>>> parsed.templates[0].arguments[0].value = 'value3'
>>> print(parsed)
{{text|value3}}

The pformat method returns a pretty-print formatted string for templates:

>>> parsed = wtp.parse('{{t1 |b=b|c=c| d={{t2|e=e|f=f}} }}')
>>> t1, t2 = parsed.templates
>>> print(t2.pformat())
{{t2
    | e = e
    | f = f
}}
>>> print(t1.pformat())
{{t1
    | b = b
    | c = c
    | d = {{t2
        | e = e
        | f = f
    }}
}}

Template.rm_dup_args_safe and Template.rm_first_of_dup_args methods can be used to clean-up pages using duplicate arguments in template calls:

>>> t = wtp.Template('{{t|a=a|a=b|a=a}}')
>>> t.rm_dup_args_safe()
>>> t
Template('{{t|a=b|a=a}}')
>>> t = wtp.Template('{{t|a=a|a=b|a=a}}')
>>> t.rm_first_of_dup_args()
>>> t
Template('{{t|a=a}}')

Template parameters:

>>> param = wtp.parse('{{{a|b}}}').parameters[0]
>>> param.name
'a'
>>> param.default
'b'
>>> param.default = 'c'
>>> param
Parameter('{{{a|c}}}')
>>> param.append_default('d')
>>> param
Parameter('{{{a|{{{d|c}}}}}}')

Sections

>>> parsed = wtp.parse("""
... == h2 ==
... t2
... === h3 ===
... t3
... === h3 ===
... t3
... == h22 ==
... t22
... {{text|value3}}
... [[Z|X]]
... """)
>>> parsed.sections
[Section('\n'),
 Section('== h2 ==\nt2\n=== h3 ===\nt3\n=== h3 ===\nt3\n'),
 Section('=== h3 ===\nt3\n'),
 Section('=== h3 ===\nt3\n'),
 Section('== h22 ==\nt22\n{{text|value3}}\n[[Z|X]]\n')]
>>> parsed.sections[1].title = 'newtitle'
>>> print(parsed)

==newtitle==
t2
=== h3 ===
t3
=== h3 ===
t3
== h22 ==
t22
{{text|value3}}
[[Z|X]]
>>> del parsed.sections[1].title
>>>> print(parsed)

t2
=== h3 ===
t3
=== h3 ===
t3
== h22 ==
t22
{{text|value3}}
[[Z|X]]

Tables

Extracting cell values of a table:

>>> p = wtp.parse("""{|
... |  Orange    ||   Apple   ||   more
... |-
... |   Bread    ||   Pie     ||   more
... |-
... |   Butter   || Ice cream ||  and more
... |}""")
>>> p.tables[0].data()
[['Orange', 'Apple', 'more'],
 ['Bread', 'Pie', 'more'],
 ['Butter', 'Ice cream', 'and more']]

By default, values are arranged according to colspan and rowspan attributes:

>>> t = wtp.Table("""{| class="wikitable sortable"
... |-
... ! a !! b !! c
... |-
... !colspan = "2" | d || e
... |-
... |}""")
>>> t.data()
[['a', 'b', 'c'], ['d', 'd', 'e']]
>>> t.data(span=False)
[['a', 'b', 'c'], ['d', 'e']]

Calling the cells method of a Table returns table cells as Cell objects. Cell objects provide methods for getting or setting each cell’s attributes or values individually:

>>> cell = t.cells(row=1, column=1)
>>> cell.attrs
{'colspan': '2'}
>>> cell.set('colspan', '3')
>>> print(t)
{| class="wikitable sortable"
|-
! a !! b !! c
|-
!colspan = "3" | d || e
|-
|}

HTML attributes of Table, Cell, and Tag objects are accessible via get_attr, set_attr, has_attr, and del_attr methods.

Lists

The get_lists method provides access to lists within the wikitext.

>>> parsed = wtp.parse(
...     'text\n'
...     '* list item a\n'
...     '* list item b\n'
...     '** sub-list of b\n'
...     '* list item c\n'
...     '** sub-list of b\n'
...     'text'
... )
>>> wikilist = parsed.get_lists()[0]
>>> wikilist.items
[' list item a', ' list item b', ' list item c']

The sublists method can be used to get all sub-lists of the current list or just sub-lists of specific items:

>>> wikilist.sublists()
[WikiList('** sub-list of b\n'), WikiList('** sub-list of b\n')]
>>> wikilist.sublists(1)[0].items
[' sub-list of b']

It also has an optional pattern argument that works similar to lists, except that the current list pattern will be automatically added to it as a prefix:

>>> wikilist = wtp.WikiList('#a\n#b\n##ba\n#*bb\n#:bc\n#c', '\#')
>>> wikilist.sublists()
[WikiList('##ba\n'), WikiList('#*bb\n'), WikiList('#:bc\n')]
>>> wikilist.sublists(pattern='\*')
[WikiList('#*bb\n')]

Convert one type of list to another using the convert method. Specifying the starting pattern of the desired lists can facilitate finding them and improves the performance:

>>> wl = wtp.WikiList(
...     ':*A1\n:*#B1\n:*#B2\n:*:continuing A1\n:*A2',
...     pattern=':\*'
... )
>>> print(wl)
:*A1
:*#B1
:*#B2
:*:continuing A1
:*A2
>>> wl.convert('#')
>>> print(wl)
#A1
##B1
##B2
#:continuing A1
#A2

Tags

Accessing HTML tags:

>>> p = wtp.parse('text<ref name="c">citation</ref>\n<references/>')
>>> ref, references = p.get_tags()
>>> ref.name = 'X'
>>> ref
Tag('<X name="c">citation</X>')
>>> references
Tag('<references/>')

WikiTextParser is able to handle common usages of HTML and extension tags. However it is not a fully-fledged HTML parser and may fail on edge cases or malformed HTML input. Please open an issue on github if you encounter bugs.

Miscellaneous

parent and ancestors methods can be used to access a node’s parent or ancestors respectively:

>>> template_d = parse("{{a|{{b|{{c|{{d}}}}}}}}").templates[3]
>>> template_d.ancestors()
[Template('{{c|{{d}}}}'),
 Template('{{b|{{c|{{d}}}}}}'),
 Template('{{a|{{b|{{c|{{d}}}}}}}}')]
>>> template_d.parent()
Template('{{c|{{d}}}}')
>>> _.parent()
Template('{{b|{{c|{{d}}}}}}')
>>> _.parent()
Template('{{a|{{b|{{c|{{d}}}}}}}}')
>>> _.parent()  # Returns None

Use the optional type_ argument if looking for ancestors of a specific type:

>>> parsed = parse('{{a|{{#if:{{b{{c<!---->}}}}}}}}')
>>> comment = parsed.comments[0]
>>> comment.ancestors(type_='ParserFunction')
[ParserFunction('{{#if:{{b{{c<!---->}}}}}}')]

To delete/remove any object from its parents use del object[:] or del object.string.

The remove_markup function or plain_text method can be used to remove wiki markup:

>>> from wikitextparser import remove_markup, parse
>>> s = "'''a'''<!--comment--> [[b|c]] [[d]]"
>>> remove_markup(s)
'a c d'
>>> parse(s).plain_text()
'a c d'

Compared with mwparserfromhell

mwparserfromhell is a mature and widely used library with nearly the same purposes as wikitextparser. The main reason leading me to create wikitextparser was that mwparserfromhell could not parse wikitext in certain situations that I needed it for. See mwparserfromhell’s issues 40, 42, 88, and other related issues. In many of those situation wikitextparser may be able to give you more acceptable results.

Also note that wikitextparser is still using 0.x.y version meaning that the API is not stable and may change in the future versions.

The tokenizer in mwparserfromhell is written in C. Tokenization in wikitextparser is mostly done using the regex library which is also in C. I have not rigorously compared the two libraries in terms of performance, i.e. execution time and memory usage. In my limited experience, wikitextparser has a decent performance in realistic cases and should be able to compete and may even have little performance benefits in some situations.

If you have had a chance to compare these libraries in terms of performance or capabilities please share your experience by opening an issue on github.

Some of the unique features of wikitextparser are: Providing access to individual cells of each table, pretty-printing templates, a WikiList class with rudimentary methods to work with lists, and a few other functions.

Known issues and limitations

  • The contents of templates/parameters are not known to offline parsers. For example an offline parser cannot know if the markup [[{{z|a}}]] should be treated as wikilink or not, it depends on the inner-workings of the {{z}} template. In these situations wikitextparser tries to use a best guess. [[{{z|a}}]] is treated as a wikilink (why else would anyone call a template inside wikilink markup, and even if it is not a wikilink, usually no harm is done).

  • Localized namespace names are unknown, so for example [[File:...]] links are treated as normal wikilinks. mwparserfromhell has similar issue, see #87 and #136. As a workaround, Pywikibot can be used for determining the namespace.

  • Linktrails are language dependant and are not supported. Also not supported by mwparserfromhell. However given the trail pattern and knowing that wikilink.span[1] is the ending position of a wikilink, it is possible to compute a WikiLink’s linktrail.

  • Templates adjacent to external links are never considered part of the link. In reality, this depends on the contents of the template. Example: parse('http://example.com{{dead link}}').external_links[0].url == 'http://example.com'

  • List of valid extension tags depends on the extensions intalled on the wiki. The tags method currently only supports the ones on English Wikipedia. A configuration option might be added in the future to address this issue.

  • wikitextparser currently does not provide an ast.walk-like method yielding all descendant nodes.

  • Parser functions and magic words are not evaluated.

Credits

3.0.0 Oct 08, 2026
2.0.0 Sep 03, 2026
1.0.3 Jul 31, 2026
1.0.2 Jun 25, 2026
1.0.1 Jun 19, 2026
1.0.0 Jun 12, 2026
0.56.4 May 14, 2025
0.56.3 Oct 18, 2024
0.56.2 Aug 03, 2024
0.56.1 Jul 19, 2024
0.56.0 Jun 28, 2024
0.55.14 Jun 28, 2024
0.55.13 Apr 19, 2024
0.55.12 Apr 12, 2024
0.55.11 Apr 09, 2024
0.55.10 Mar 19, 2024
0.55.9 Mar 11, 2024
0.55.8 Jan 15, 2024
0.55.7 Dec 21, 2023
0.55.6 Nov 25, 2023
0.55.5 Nov 08, 2023
0.55.4 Nov 07, 2023
0.55.3 Nov 07, 2023
0.55.2 Nov 05, 2023
0.55.1 Nov 05, 2023
0.55.0 Nov 05, 2023
0.54.1 Nov 03, 2023
0.54.1.dev0 Nov 03, 2023
0.54.0 Aug 14, 2023
0.53.0 Jul 07, 2023
0.52.1 May 19, 2023
0.52.0 May 19, 2023
0.51.2 Apr 21, 2023
0.51.1 Oct 14, 2022
0.51.0 Sep 16, 2022
0.50.2 Sep 10, 2022
0.50.1 Aug 29, 2022
0.50.0 Aug 29, 2022
0.49.4 Jul 28, 2022
0.49.3 Jul 04, 2022
0.49.2 May 20, 2022
0.49.1 Apr 11, 2022
0.49.0 Apr 11, 2022
0.48.3 Apr 08, 2022
0.48.2 Mar 09, 2022
0.48.1 Mar 05, 2022
0.48.0 Dec 31, 2021
0.47.10.dev2 Dec 31, 2021
0.47.10.dev1 Dec 31, 2021
0.47.10.dev0 Dec 31, 2021
0.47.9 Nov 26, 2021
0.47.8 Nov 19, 2021
0.47.7 Nov 11, 2021
0.47.6 Nov 05, 2021
0.47.5 Jun 03, 2021
0.47.4 Mar 21, 2021
0.47.3 Feb 14, 2021
0.47.2 Feb 13, 2021
0.47.1 Feb 05, 2021
0.47.0 Nov 28, 2020
0.46.0 Oct 14, 2020
0.45.3 Oct 11, 2020
0.45.2 Sep 30, 2020
0.45.1 Sep 25, 2020
0.45.0 Sep 14, 2020
0.44.1 Sep 10, 2020
0.44.0 Aug 28, 2020
0.43.2 Aug 19, 2020
0.43.1 Aug 14, 2020
0.43.0 Aug 14, 2020
0.42.3 Aug 12, 2020
0.42.2 Aug 04, 2020
0.42.1 Jul 19, 2020
0.42.0 Jul 19, 2020
0.41.0 Jul 12, 2020
0.40.0 Jul 12, 2020
0.39.0 Jul 12, 2020
0.38.2 Jul 09, 2020
0.38.1 Jul 09, 2020
0.38.0 Jul 07, 2020
0.37.12 Jul 02, 2020
0.37.11 Jul 01, 2020
0.37.10 Jul 01, 2020
0.37.9 Jul 01, 2020
0.37.8 Jul 01, 2020
0.37.7 Jun 30, 2020
0.37.6 Jun 30, 2020
0.37.5 Jun 29, 2020
0.37.4 Jun 29, 2020
0.37.3 Jun 26, 2020
0.37.2 Jun 21, 2020
0.37.1 Jun 11, 2020
0.37.0 Jun 06, 2020
0.37.0.dev1 Jun 05, 2020
0.36.1 May 18, 2020
0.35.2 May 18, 2020
0.35.1 May 18, 2020
0.35.0 May 02, 2020
0.34.0 Mar 09, 2020
0.33.0 Mar 09, 2020
0.32.0 Feb 26, 2020
0.31.0 Feb 25, 2020
0.30.0 Feb 18, 2020
0.29.2 Feb 16, 2020
0.29.1 Jan 31, 2020
0.29.0 Jan 31, 2020
0.28.1 Nov 07, 2019
0.28.0 Aug 07, 2019
0.27.0 Aug 05, 2019
0.26.1 Jun 08, 2019
0.26.0 May 06, 2019
0.25.1 May 05, 2019
0.25.1.dev0 May 05, 2019
0.25.0 May 05, 2019
0.24.4 May 03, 2019
0.24.3 Apr 14, 2019
0.24.2 Apr 14, 2019
0.24.1 Apr 02, 2019
0.24.0 Mar 25, 2019
0.23.0 Mar 20, 2019
0.22.1 Feb 01, 2019
0.22.1.dev0 Aug 31, 2018
0.22.0 Aug 31, 2018
0.21.5 May 29, 2018
0.21.4 Apr 02, 2018
0.21.3 Mar 30, 2018
0.21.2 Mar 08, 2018
0.21.2.dev0 Mar 07, 2018
0.21.0 Mar 06, 2018
0.20.0 Feb 10, 2018
0.19.0 Feb 03, 2018
0.18.0 Jan 30, 2018
0.18.0.dev0 Jan 30, 2018
0.17.4 Jan 26, 2018
0.17.3 Dec 31, 2017
0.17.3.dev0 Dec 31, 2017
0.17.1 Jul 19, 2017
0.17.0 Jul 19, 2017
0.16.1 Jul 11, 2017
0.16.0 Jul 08, 2017
0.15.2 Jul 08, 2017
0.15.1 Jun 04, 2017
0.15.0 May 20, 2017
0.14.3 Feb 18, 2017
0.14.3.dev1 Feb 13, 2017
0.14.1 Feb 09, 2017
0.14.0 Feb 07, 2017
0.13.6 Jan 28, 2017
0.13.5 Jan 09, 2017
0.13.4 Jan 04, 2017
0.13.2 Dec 27, 2016
0.13.1 Dec 27, 2016
0.13.0 Dec 26, 2016
0.13.0.dev1 Dec 26, 2016
0.12.0 Dec 15, 2016
0.11.1.dev4 Dec 10, 2016
0.11.1.dev3 Dec 10, 2016
0.11.1.dev2 Dec 10, 2016
0.11.1.dev1 Dec 08, 2016
0.11.0 Dec 05, 2016
0.10.2 Nov 13, 2016
0.10.1 Nov 10, 2016
0.10.0 Nov 10, 2016
0.10.0.dev2 Nov 10, 2016
0.10.0.dev1 Nov 09, 2016
0.9.1 Nov 02, 2016
0.9.0 Oct 26, 2016
0.9.0.dev1 Oct 26, 2016
0.8.8.dev1 Oct 26, 2016
0.8.6.dev1 Oct 24, 2016
0.8.5.dev1 Oct 24, 2016
0.8.3 Sep 27, 2016
0.8.3.dev0 Sep 27, 2016
0.8.2 Sep 24, 2016
0.8.1 Sep 17, 2016
0.8.0 Aug 11, 2016
0.7.9 Jul 28, 2016
0.7.8 Jul 22, 2016
0.7.7 Jul 20, 2016
0.7.6 May 30, 2016
0.7.5 Apr 11, 2016
0.7.4 Mar 03, 2016
0.7.3 Feb 24, 2016
0.7.2 Feb 24, 2016
0.7.1 Feb 21, 2016
0.7.0 Feb 15, 2016
0.6.9 Nov 18, 2015
0.6.8 Nov 09, 2015
0.6.7 Nov 08, 2015
0.6.6 Nov 08, 2015
0.6.5 Nov 06, 2015
0.6.4 Nov 06, 2015
0.6.3 Oct 30, 2015
0.6.2 Oct 30, 2015
0.6.1 Oct 21, 2015
0.6.0 Oct 20, 2015
0.5.9 Oct 20, 2015
0.5.8 Oct 20, 2015
0.5.7 Oct 15, 2015
0.5.6.dev0 Oct 15, 2015
0.5.5 Sep 30, 2015
0.5.3 Jun 24, 2015
0.5.2 Jun 24, 2015
0.5.1 Jun 24, 2015
0.4.8 Jun 20, 2015
0.4.7 Jun 19, 2015
0.4.6 Jun 16, 2015
0.4.5 May 20, 2015
0.4.4 May 18, 2015
0.4.3 May 17, 2015
0.4.2 May 02, 2015
0.4.1 May 01, 2015
0.4 Apr 26, 2015
0.3.1 Apr 21, 2015
0.2 Apr 16, 2015
0.1.3 Apr 10, 2015
0.8.3dev.win Sep 27, 2016
0.8.3.win Sep 27, 2016
0.8.2.win Sep 24, 2016
0.8.1.win Sep 17, 2016
0.8.0.win Aug 11, 2016
0.7.9.win Jul 28, 2016
0.7.8.win Jul 22, 2016
0.7.7.win Jul 20, 2016
0.7.6.win May 30, 2016
0.7.5.win32 Apr 11, 2016
0.7.4.win32 Mar 03, 2016
0.7.3.win32 Feb 24, 2016
0.7.0.win32 Feb 15, 2016
0.6.9.win32 Nov 18, 2015
0.6.8.win32 Nov 09, 2015
0.6.7.win32 Nov 08, 2015
0.6.6.win32 Nov 08, 2015
0.6.4.win32 Nov 06, 2015
0.6.3.win32 Oct 30, 2015
0.6.2.win32 Oct 30, 2015
0.6.1.win32 Oct 21, 2015
0.6.0.win32 Oct 20, 2015
0.5.9.win32 Oct 20, 2015
0.5.8.win32 Oct 20, 2015
0.5.7.win32 Oct 15, 2015
0.5.6.dev0.win32 Oct 15, 2015
0.5.5.win32 Sep 30, 2015
0.5.3.win32 Jun 24, 2015
0.5.2.win32 Jun 24, 2015
0.5.1.win32 Jun 24, 2015
0.4.8.win32 Jun 20, 2015
0.4.7.win32 Jun 19, 2015
0.4.6.win32 Jun 16, 2015
0.4.5.win32 May 20, 2015
0.4.4.win32 May 18, 2015
0.4.3.win32 May 17, 2015
0.4.2.win32 May 02, 2015
0.4.1.win32 May 01, 2015
0.3.1.win32 Apr 21, 2015
0.2.win32 Apr 16, 2015
0.1.3.win32 Apr 10, 2015

Wheel compatibility matrix

Platform Python 3
any

Files in release

Extras:
Dependencies:
regex (>=2022.9.11)
wcwidth