import pywikibot
from pywikibot import pagegenerators as pg

#bij een achternaam PQ101352 met voorvoegsel (de Jong, van der Velde, van de Wetering, van Willigenburg, etc)

sqlQuery='select ?item where {?item wdt:P31 wd:Q101352}'
searchfor=['de ','van der ','van de ','van ','in het ']
someitems=['Q21494168','Q1180481','Q7913814']
lang='nl'

def add2list(list,item2add,changed):
  if not(item2add in list):
    list.append(item2add)
    return list,True
  return list,changed  

def wd_sparql_query(spq):
  wikidatasite=pywikibot.Site('wikidata','wikidata') 
  generator=pg.WikidataSPARQLPageGenerator(spq,site=wikidatasite)
  for wd in generator:
    try:
      wd.get(get_redirect=True)
      yield wd
    except:
      pass  

def action_one_item(wd):
  changed=False
  alias=[]
  if lang in wd.aliases:
    for onealias in wd.aliases[lang]:
      alias,changed =add2list(alias,onealias,changed)
  changed=False  
  if lang in wd.labels:
    label=wd.labels[lang]
    for found in searchfor:
      if (label[0:len(found)].lower()==found.lower()):
        alias,changed=add2list(alias,label[len(found):]+' '+found,changed)
        label='   ' #so it won't get another alias
    if (changed):
        newalias=[]
        for onealias in alias:
            newalias.append(onealias)
        data={}
        data.update({'aliases':{lang:newalias}})
        wd.editEntity(data,summary=f'achternaam-alias <{newalias}>')
  return(changed)

print('Begonnen')      
aantal=0
site = pywikibot.Site('wikidata','wikidata')
repo = site.data_repository()

if (False):
    for item in someitems:
      wd=pywikibot.ItemPage(repo,item)
      wd.get()
      if (action_one_item(wd)):
          aantal+=1
          print('x: %d: %s-%s' % (aantal,item.title(),ifany))
else:    
  for item in wd_sparql_query(sqlQuery):
    if (action_one_item(item)):
      ifany=''
      if (lang in item.labels):
        ifany=item.labels[lang]
      print('x: %d: %s-%s' % (aantal,item.title(),ifany))
      aantal+=1
    #if aantal>250: break
    
print('Klaar')    
Begonnen
x: 0: Q5244318-De Cock
Sleeping for 6.6 seconds, 2019-01-26 18:29:56
x: 1: Q60168933-De Hert
x: 2: Q31210809-de Montchalin
x: 3: Q44677628-de Fehrt
x: 4: Q48981913-De Moortel
x: 5: Q7913366-Van Gyseghem
x: 6: Q55636947-Van der Hagen
Sleeping for 6.1 seconds, 2019-01-26 18:34:24
x: 7: Q7913815-Van den Bergh
Sleeping for 3.6 seconds, 2019-01-26 18:34:36
x: 8: Q40189324-De Ceulener
x: 9: Q5244659-De Nooijer
x: 10: Q47457527-de Flon
x: 11: Q7913621-Van Riemsdijk
x: 12: Q7913222-Van Buskirk
x: 13: Q35488605-de Quattro
x: 14: Q16869454-De Nijs
x: 15: Q49969742-de Ployart
Sleeping for 9.1 seconds, 2019-01-26 18:37:50
x: 16: Q31212385-de Lavergne
x: 17: Q56546853-Van Beuningen
x: 18: Q48370593-de Thurah
x: 19: Q29559637-de Ladmirault
Sleeping for 7.8 seconds, 2019-01-26 18:39:59
x: 20: Q7913884-Van der Vorst
x: 21: Q16802351-De Santi
x: 22: Q21508022-De Hooch
Sleeping for 7.0 seconds, 2019-01-26 18:42:28
x: 23: Q7913418-Van Hooydonk
x: 24: Q23781416-de Rocca Serra
x: 25: Q54273153-Van Dorn
x: 26: Q27256300-De Wilde
x: 27: Q28122799-de Dieuleveult
Sleeping for 3.8 seconds, 2019-01-26 18:45:52
x: 28: Q47463498-de Jounge
Sleeping for 3.1 seconds, 2019-01-26 18:46:03
x: 29: Q2124017-Van Lippe-Biesterfeld van Vollenhoven
x: 30: Q53703358-De Pape
x: 31: Q28800649-de Dios
x: 32: Q7913593-Van Putten
x: 33: Q41363992-de Meyere
x: 34: Q23776846-de La Raudière
x: 35: Q7913753-Van Vuuren
x: 36: Q44686163-Van Deurs
x: 37: Q48979653-De Swardt
x: 38: Q7913698-Van Tilburg
x: 39: Q35492160-de Cassis
Sleeping for 3.3 seconds, 2019-01-26 18:51:42
x: 40: Q28925623-de Moura
Sleeping for 7.9 seconds, 2019-01-26 18:51:47
x: 41: Q27265358-Van Bossche
x: 42: Q27136313-Van Der Stricht
x: 43: Q41364148-de Sala
import pywikibot

site = pywikibot.Site('wikidata','wikidata')
repo = site.data_repository()
mypage=pywikibot.ItemPage(repo,'Q101352')
mypage.get()

for x in dir(mypage):
    print(x)
__class__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattribute__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__sizeof__
__str__
__subclasshook__
__unicode__
__weakref__
_cache_attrs
_cmpkey
_content
_cosmetic_changes_hook
_defined_by
_diff_to
_getInternals
_get_parsed_page
_isredir
_latest_cached_revision
_link
_namespace
_normalizeData
_normalizeLanguages
_pageid
_revid
_revisions
_save
addClaim
aliases
applicable_protections
aslink
autoFormat
backlinks
botMayEdit
canBeEdited
categories
change_category
claims
clear_cache
concept_uri
concept_url
content_model
contributingUsers
contributors
coordinates
data_item
data_repository
defaultsort
delete
depth
descriptions
editAliases
editDescriptions
editEntity
editLabels
editTime
embeddedin
encoding
entity_type
exists
expand_text
extlinks
fromPage
from_entity_uri
fullVersionHistory
full_url
get
getCategoryRedirectTarget
getCreator
getDeletedRevision
getID
getLatestEditors
getMovedTarget
getOldVersion
getRedirectTarget
getReferences
getRestrictions
getSitelink
getTemplates
getVersionHistory
getVersionHistoryTable
getdbName
id
image_repository
imagelinks
interwiki
isAutoTitle
isCategory
isCategoryRedirect
isDisambig
isEmpty
isFlowPage
isImage
isIpEdit
isRedirectPage
isStaticRedirect
isTalkPage
is_categorypage
is_filepage
is_flow_page
is_valid_id
iterlanglinks
iterlinks
itertemplates
labels
langlinks
lastNonBotUser
latestRevision
latest_revision
latest_revision_id
linkedPages
loadDeletedRevisions
markDeletedRevision
mergeInto
merge_history
move
moved_target
namespace
oldest_revision
pageAPInfo
page_image
pageid
permalink
preloadText
previousRevision
previous_revision_id
properties
protect
protection
purge
put
put_async
removeClaims
removeSitelink
removeSitelinks
repo
revision_count
revisions
save
section
sectionFreeTitle
setSitelink
setSitelinks
set_redirect_target
site
sitelinks
templates
text
title
titleForFilename
titleWithoutNamespace
title_pattern
toJSON
toggleTalkPage
touch
undelete
urlname
userName
version
watch