Wikidata:Pywikibot - Python 3 Tutorial/Create Items

If an Item can not be found the bot can also create it. It is important that the bot makes sure that the item is really necessary. Otherwise it will create many duplicates.

A new item can be created with this function:

import pywikibot
site = pywikibot.Site("test", "wikidata")

def create_item(site, label_dict):
    new_item = pywikibot.ItemPage(site)
    new_item.editLabels(labels=label_dict, summary="Setting labels")
    # Add description here or in another function
    return new_item.getID()

some_labels = {"en": "Hamburg Main Station", "de": "Hamburg Hauptbahnhof"}
new_item_id = create_item(site, some_labels)

Note that the function returns the item-id and not the ItemPage object. If you do anything else with the item, you have to call the ItemPage again using the ID. It is also a good idea to set labels and descriptions in many languages, so people can find the new item. Adding statements to new items is also important, as items without statements can be deleted.