Manual:Pywikibot/Cookbook/Creating pages based on a pattern

Pywikibot is your friend when you want to create a lot of pages that follow some pattern. In the first task we create more than 250 pages in a loop. Then we go on to categories. We prepare a lot of them, but create only as many in one run that we want to fill with articles, in order to avoid a lot of empty categories.

Rules of orthography
Rules of Hungarian orthography have 300 points, several of which have a lot of subpoints marked with letters. There is no letter a without b, and last letter is l. We have templates pointing to these on an outer source. Templates cannot be used in an edit summary, but inner links can, so we create a lot of pages with short inner links that hold these templates. Of course, bigger part is a bot work, but first we have to list the letters. Each letter from b to l gets a list with the numbers of points of which this is the last letter (lines 5–12). For example, 120 is in the list of e, so we create the pages for the 120, 120 a) ... 120 e) points. The idea is to build a title generator (from line 14). (It also could be a page generator, but title was more comfortable.)

The result is at hu:Wikipédia:AKH. marks the actual subpage which has, while   with   the main page. As we get the titles from the iterator (line 41–), we create the text with the appropriate template and a standard part, we create the page, and add its link to the text of the main page. At the end we save the main page (line 63–).

Categories of notable pupils and teachers
We want to create categories for famous pupils and teachers of Budapest schools based on a pattern. Of course, this is not relevant for each school; first we want to see which article has "famous pupils" and "famous teachers" section which may occur in several forms, so the best thing is to review it by eyes. We also check if the section contains enough notable people to have a category.

In this task we don't bother creating Wikidata items; these categories are huwiki-specific, and creating items in Wikidata by bot needs an approval.


 * Step 1 – list the section titles of the schools onto a personal sandbox page
 * We use the  function from   to get the titles. This returns a NamedTuple in which   holds the sections as   tuples, from which element   is the desired title with its   signs.
 * Note that  is not a method of a class, just a function, thus it is not aware of , and must explicitely get it.
 * The result is here.


 * Step 2 – manual work
 * We go through the schools, remove the unwanted and the subtitles, and mark with  after the title if we want to create categories both for pupils and teachers, and with , if only for pupils. There could be also a  , but isn't. This is the result.
 * We don't want to create a few dozens of empty categories at once because the community may not like it. Rather, we mark the schools we want to work on soon with the beginning of the desired category name and the sortkey, as shown here, and the bot will create the categories if the name is present and the category does not exist yet.


 * If you don't like the syntax used here,never mind, it's up to you. This is just an example, you can create and parse any syntax, any delimiters.


 * Step 3 – creating the categories
 * We read the patterns from the page with a regex, parse them, and create the name and content of the category page (including sortkey within the parent category).
 * The script creates a common category, then one for the pupils, and then another for the teachers only if necessary.
 * Next time we can add new names to schools on which we want to work that day; the existing categories will not be changed or recreated.