import pywikibot
pywikibot.config.family = "commons"
pywikibot.config.mylang = "commons"

category_to_add = 'Category:Members of the 1st Congress of Deputies'
mypages = ['Category:Marta Mata']
for mypage in mypages:
    current_category = pywikibot.Category(pywikibot.Site('commons'), mypage)
    current_category_memberships = list(current_category.categories())
    for current_category_membership in current_category_memberships:
        print(current_category_membership.text)
{{birthcat|192|6}}
{{Wikidata Infobox}}
{{deathcat2|20|0|6}}
{{Wikidata Infobox}}
{{Countries of Europe|prefix=:Category:20th-century women of|suffix=}}
[[File:LocationSpain.svg|thumb|120px|right|<small>{{LangSwitch|ca=Localització|en=Location|es=Localización|eu=Kokapena|fr=Emplacement|gl=Localización}}</small>]]
{{clr}}
[[Category:20th-century people of Spain|*Women]]
[[Category:20th-century women by country|Spain]]
[[Category:Women of Spain by century]]
{{cat see also|Deaths in Barcelona}}
{{Wikidata Infobox}}
[[Category:Births in Spain by city|Barcelona]]
[[Category:Births in the province of Barcelona| Barcelona]]
[[Category:People of Barcelona|°]]
:See also: {{W|Wikipedia:Database reports/Recent deaths}}

{{CatCat}}
{{hiddencat}}

<center>{{LargeCategoryTOC}}</center>

[[Category:People by name by vital status]]
[[Category:Dead people| ]]
{{Wikidata Infobox}}
[[Category:Educators from Spain| Catalonia]]
[[Category:Education in Catalonia]]
[[Category:People of Catalonia by occupation]]
[[Category:Educators|#Female]]
[[Category:Women by occupation|Educators]]
[[Category:Women and education]]
[[Category:Educators from Spain| ]]
[[Category:Women of Spain by occupation|Educators]]
[[Category:Female educators by country|Spain]]
{{Regions of Spain|prefix=:Category:Female politicians of|suffix=}}
[[File:Localización de Cataluña.svg|thumb|90px|right|<small>{{LangSwitch|ca=Localització|en=Location|es=Localización|eu=Kokapena|fr=Emplacement|gl=Localización}}</small>]]
{{Nav-Cat}}{{clr}}


[[Category:Politicians of Catalonia|*Female politicians]]
[[Category:Women of Catalonia by occupation|Politicians]]
[[Category:Female politicians of Spain by autonomous community|Catalonia]]
{{Wikidata Infobox}}

[[Category:Surnames]]
[[Category:Spanish surnames]]
{{Wiktionary|Marta}}
{{Wikidata Infobox}}

[[Category:Female names]]
[[Category:Portuguese-language feminine given names]]
[[Category:Spanish-language feminine given names]]
[[Category:Catalan-language feminine given names]]
[[Category:Polish-language feminine given names]]
{{Wikidata Infobox}}
[[Category:Spanish surnames]]
[[Category:Surnames]]
{{Wikidata Infobox}}
[[Category:Members of the Congress of Deputies of Spain| 01]]
[[Category:1st Legislature of Spain]]
{{Wikidata Infobox}}
[[Category:Politicians of Catalonia by party|PSC]]
[[Category:Partit dels Socialistes de Catalunya]]
[[Category:Politicians of Spain by party|Partit dels Socialistes de Catalunya]]
{{LangSwitch
|en = {{en|All categories named after a person should be included in this category and sorted by their family name. This is a "flat" category so please do not build category trees here. Sorting can be done with one of the following:
*<code><nowiki>{{DEFAULTSORT:Family name, Given name}}</nowiki><br /><nowiki>[[Category:People by name]]</nowiki></code>
*<code><nowiki>[[Category:People by name|Family name, Given name]]</nowiki></code>
}}
|de = {{de|'''ANMERKUNG:''' Jede Person ist in dieser Kategorie in der Form 
*<code><nowiki>[[Category:People by name|Nachname, Vorname]]</nowiki></code>
*<code><nowiki>[[Category:People by name]] {{DEFAULTSORT:Nachname, Vorname}}</nowiki></code>
einzuordnen. In dieser Kategorie keine Bilder.}}
|es = {{es|'''ADVERTENCIA:''' Cada persona debe ser ordenada en esta categoría usando
*<code><nowiki>[[Category:People by name|Apellidos, Nombre]]</nowiki></code>
*<code><nowiki>[[Category:People by name]] {{DEFAULTSORT:Apellidos, Nombre}}</nowiki></code>
No categorices imágenes aquí.}}
|fr = {{fr|'''NOTE :''' Chaque personnalité doit être ordonnée dans cette catégorie au format *<code><nowiki>[[Category:People by name|Nom de famille, Prénom]]</nowiki></code>
*<code><nowiki>[[Category:People by name]] {{DEFAULTSORT:Nom de famille, Prénom}}</nowiki></code>
Aucune image dans cette catégorie.}}
|fy = {{fy|Alle kategoryen en galeryen dy't nei in persoan neamd binne, soenen op efternamme yn dizze kategory opnommen wurde moatte:
*<code><nowiki>[[Category:People by name|Efternamme, Foarnamme]]</nowiki></code>
*<code><nowiki>[[Category:People by name]] {{DEFAULTSORT:Efternamme, Foarnamme}}</nowiki></code>
''Pleats gjin ôfbyldings yn dizze kategory sels.''}} 
|it = {{it|'''NOTA: '''Ogni persona deve essere ordinata in questa categoria nella forma
*<code><nowiki>[[Category:People by name|Cognome, Nome]]</nowiki></code>
*<code><nowiki>[[Category:People by name]] {{DEFAULTSORT:Cognome, Nome}}</nowiki></code>
Non ci devono essere immagini in questa categoria.}}
|nl = {{nl|Alle categorieën en galerijen die naar een persoon zijn genoemd, zouden op achternaam in deze categorie moeten worden opgenomen:
*<code><nowiki>[[Category:People by name|Achternaam, Voornaam]]</nowiki></code>
*<code><nowiki>[[Category:People by name]] {{DEFAULTSORT:Achternaam, Voornaam}}</nowiki></code>
''Voeg geen afbeeldingen rechtstreeks aan deze categorie toe.''}} 
|pl = {{pl|'''UWAGA:''' Wszystkie kategorie i galerie nazwane od osoby powinny być włączane do tej kategorii i segregowane według nazwiska: <code><nowiki>[[Category:People by name|Nazwisko, Imię]]</nowiki></code>. ''Do tej kategorii nie należy bezpośrednio dodawać grafik.''}}
|pt = {{pt|'''AVISO:''' Cada pessoa deve ser ordenada nesta categoria usando <nowiki>[[Category:People by name|Apelidos, Nome]]</nowiki>. Não categorizes imagens aqui.}}
|ru = {{ru|'''ВНИМАНИЕ:''' Каждая персона вносится в эту категорию формой <nowiki>[[Category:People by name|Фамилия (второе имя), Имя]]</nowiki>. Никаких изображений.}}
|sk = {{sk|'''UPOZORNENIE:''' Všetky kategórie a galérie nazvané podľa osoby by mali náležať do tejto kategórie a mali by byť usporiadané podľa priezviska: <code><nowiki>[[Category:People by name|Priezvisko, Meno]]</nowiki></code>. ''Do tejto kategórie nepatria bezprostredne obrázky.''}}
|fi = {{fi|1=Kaikki henkilön mukaan nimetyt luokat ja galleriat tulisi sijoittaa tähän luokkaan sukunimen mukaan aakkostettuna: <code><nowiki>[[Category:People by name|Sukunimi, Etunimi]]</nowiki></code>. ''Älä lisää kuvia suoraan tähän luokkaan.''}}
|sv = {{sv|Alla kategorier och gallerier som är uppkallade efter en person bör inkluderas i denna kategori och sorteras efter familjenamn: <code><nowiki>[[Category:People by name|Familjenamn, Tilltalsnamn]]</nowiki></code>. ''Lägg inte till bilder direkt i denna kategori.''}}
|da = {{da|Alle kategorier (eller gallerier i tilfælde hvor der ikke er en tilsvarende kategori), som er opkaldt efter en person, bør inkluderes i denne kategori og sorteres efter efternavn. Dette kan gøres på en af følgende måder:
*<code><nowiki>{{DEFAULTSORT:Efternavn, For- og mellemnavn}}</nowiki><br /><nowiki>[[Category:People by name]]</nowiki></code>
*<code><nowiki>[[Category:People by name|Efternavn, For- og mellemnavn]]</nowiki></code>
''Læg ikke billeder direkte i denne kategori.''}}
}}
{{clear}}
<center>{{LargeCategoryTOC}}</center>
{{FlatCat}}
{{catcat}}
{{Wikidata Infobox}}
__HIDDENCAT__
[[Category:Categories by name]]
[[Category:People categories by name| ]]
[[Category:Banyeres del Penedès]]
[[Category:People of Baix Penedès|Banyeres]]
[[Category:People of Catalonia by city|banyeres]]
{{Countries of Europe|prefix=:Category:People of|suffix=by name}}

{{catcat}}
{{hiddencat}}
{{CategoryTOC}}

[[Category:People of Spain| name]]
[[Category:People by name by country|Spain]]
[[Category:Categories of Spain by name]]
{{Provinces of Spain|prefix=:Category:People of|suffix=}}
[[File:Localización de la provincia de Tarragona.svg|thumb|90px|right|<small>{{LangSwitch|ca=Localització|en=Location|es=Localización|eu=Kokapena|fr=Emplacement|gl=Localización}}</small>]]
{{Nav-Cat}}{{clr}}
{{Wikidata Infobox}}

[[Category:People of Spain by province|Tarragona]]
[[Category:People of Catalonia by province|Tarragona]]
[[Category:Province of Tarragona]]

[[fr:Catégorie:Naissance dans la province de Tarragone]]
[[pt:Categoria:Naturais da província de Tarragona]]
{{Provinces of Spain|prefix=:Category:Politicians of|suffix=}}

[[File:Localización de la provincia de Tarragona.svg|thumb|100px|right|<small>{{LangSwitch|ca=Localització|en=Location|es=Localización|eu=Kokapena|fr=Emplacement|gl=Localización}}</small>]]
{{clr}}

[[Category:Politicians of Catalonia by province|Tarragona]]
[[Category:People of the province of Tarragona|*Politicians]]
{{autocat|Wikidata Infobox}}
{{Hiddencat}}
[[Category:Wikidata infobox maintenance]]
{{LargeCategoryTOC|numbers=y}}
{{autocat|Wikidata Infobox}}
{{Empty category}}
{{Hiddencat}}
{{LargeCategoryTOC}}
[[Category:Wikidata infobox maintenance]]
<center>{{LargeCategoryTOC}}</center>
{{FlatCat}}
{{CatCat}}
{{hiddencat}}
{{Wikidata Infobox}}

[[Category:Women| Name]]
[[Category:People by name by gender]]
for mypage in mypages:
    category = pywikibot.Category(pywikibot.Site('commons'), mypage)
    subcategories = list(category.categories())
    print(subcategories)
    for subcategory in subcategories:
        if category_to_add == subcategory.text:
            print('already existing')
        else:
            print('not matched in '+subcategory.text)