Help needed: How to rename mass of tags?

I have currently the problem, that I have to rename all my structured tags in my library (and my company intern group libraries) according a new company wide style. Doing this by hand in the tag-window is possible but would be an enormous work.

An example: I have tags with the following layout A:project>nr>E20145 and the final layout should be >project>nr>E20145. I have to search-replace all "A:" by ">" in all tags.

Is there an easy way doing this directly and more efficently as renaming it by hand?
What is the best way herefore?

Thanks for any help or suggestions.

  • What version of Zotero are you running?
  • zotero5 standalone - with the firefox connector.
    (But some of the group work with zotero4 as firefox plugin)
  • On Zotero 4, this works:

    On Zotero 5, easiest would likely be to go through the server API using Pyzotero
  • In Zotero4 for Firefox, you could use the Zotero JavaScript API to rename the tags using regular expressions:
  • Thanks to both.

    I'm using zotero5, therefore I should try pyzotero.

    Do the zotero javascript method don't work for zotero5 anymore?

    I'm not really familiar with python / javascript and the zotero API. Do you know a step-by-step example, I can follow (perhaps for the pyzotero way)?
  • The Zotero javascript method works in theory in Zotero 5, but there's no good way to access it, since the Execute.js add-on doesn't work in Zotero 5.
  • adamsmith, thanks for the information.

    I will try to work with pyzotero. Any help for this way is welcome ;-).
  • I have tried to use pyzotero. Initial steps were successfully. I can find, add and delete tags from a group library.

    But I can't find an API method to modify a tag.
    Is there any simple way to modify the contents of a tag with a regexp command?
  • I don't think pyzotero (or the Zotero API for that matter) has an edit tag function -- I'd apply the new tag to all items with the old one and then delete the old one. Does that make sense?
  • Yes it makes sense. Thanks.
    It is a bit complicated for a non-python man as me. But I will try it.

    Perhaps it would be an simple zotero API extension to have a method to modify a tag. I think it would be very similar to the delete_tag method.
  • @adamsmith
    Now I have written a python script, which adds renamend tags and finally deletes the old one. It works so far.

    In the group library at the Zotero-Server the "deleted" tags seemed to be deleted.

    But in my "local" group lib, the "deleted" tags are still there. Also after sync. Also after cleaning the trash. Also after restarting zotero5.

    What is going wrong?
  • Are they remaining on the items, or only in the tag selector? If the latter, they should disappear in a short time after no more items have them.
  • Both - at the item and in the tag selector.
  • That shouldn't be possible. What's the group's name? @dstillman would have to look at that.
  • The group name is "EL-Gruppe" (ID: 236494).

    I have tried to replace all tags with "A:..." by ">".

    In my local EL-Gruppe Lib I have an item with both tags e.g. A:proj>R>TUS-BurstWave-Analyse and the new replacement >proj>R>TUS-BrustWave-Analyse. The tag-selector shows both tags.

  • Tag purging doesn't happen online, so you might need to delete a tag locally to trigger tag purging. (You can create a new temporary tag and delete it.)
  • Thanks for your response.

    That means, that the pyzotero method "zotero.delete_tags" do not work properly?

    Do I understand you right, you advice me to manually delete the "undeleted" tags in the tag-selector?

  • No, it's just that the right sync operation isn't triggered. If you delete any tag locally, the synced tag will sync down
  • Actually, I was describing something else (a deleted tag showing up in the tag selector), but that's not the issue here. @SiGi, I'm a bit confused, because it looks like there are still lots of "A:" tags in your online library. What the item key for an item where you're seeing different results online and locally?

    Also, what underlying API requests are you using? Generally the way to change a tag via the API would be to make item writes that simply swap one tag for another.
  • @dstillman, you are right. Now I have the same A: tags online as I have locally. As I have started the discussion, this situation was different.

    For tag-renaming I use pyzotero and the following python-skript. Initially I didn't used the zotero.everything method - so I got perhaps only 50 search results and not all (perhaps was that a problem).

    Here the script I use now:
    from pyzotero import zotero
    zot = zotero.Zotero(libnummer, libtype,libapikey )
    worte = zot.everything(zot.tags(q="A:")) ##############EDIT
    for wort in worte:
    wortneu = wort.replace("A:", ">") ########## EDIT
    print('Ersetze: %s -> %s' % (wort, wortneu))
    # items mit dem zu ersetzenden Tag finden
    items = zot.everything(zot.items(tag=wort))
    # In allen gefundenen Items den den neuen Tag hinzufügen
    # Alter Tag wird NICHT hier gelöscht (er muss global gelöscht werden)
    for item in items:
    print('Item Type: %s | Key: %s' % (item['data']['itemType'], item['data']['key']))
    # Neuer Tag zum aktuellem Item hinzufügen
    # Alle alten Tags (A:) aus Lib löschen
    for wort in worte:
    print('Lösche: %s' % wort)
    updated = zot.delete_tags(wort)

  • @dstillman, now I have used the script above another time for my EL-Gruppe group library. It works without error.
    But after manual sync, it seems to be the same situation as before: locally I have some item with A:-Tags (also in the tag-selector) and online all A:-Tags are removed.
    e.g. the item: "Zotero - Home": locally with A:prod>Zotero,... online without A:-Tag and without >prod>Zotero
  • @dstillman, could it be, that there is a "sync" problem.

    When I change tags (add/delete them) over pyzotero (zotero API) it takes some time (about 1min) until I see the changes in the web-version of the group lib. I have to refresh the tag-selector (web-version) by hand. Reloading the web-page is not sufficient. For some time the tags are deleted in the tag-selector but still visible at the items. After additional time the tags are also deleted at the items (in the web version).

    But nothing happens in my local group library! I sync manually from time to time (10min afterwards, 30min afterwards, 1day afterwards) but nothing happens. The deleted tags stay visible in the tag selector and at the items.

    Is it possible, that the local version do not really see, that there are changes made at the server, because I have done them with pyzotero/zotero API?

  • Sorry for the delay. This should now be fixed. Unfortunately there's no easy way to sync those changes back from the API at this point (and if you make other changes to the items locally the tags will sync back up), so you'll have to either reapply those tag deletions locally or add another tag to them via the API so the new versions sync down.
  • Thanks @dstillman. Your last hint to add another tag to force down sync to the mass-edited tags via the pyzotero API works.
  • edited June 11, 2019
    Thanks for this interesting discussion and sharing your scripts. I see this post is a bit older but I hope Pyzotero is still compatible with the latest Zotero version as I also need to rename/unify tags in my library. If possible, I would also like to add new tags to all items in one folder/collection. Is there any complete "Pyzotero" tutorial I might consult? As for @SiGi's, script, I was wondering where libnummer, libtype and libapikey are coming from. In my Zotero settings, I was able to retrieve a userID for API calls. It is a seven-digit number. Is that what I will need to put in there as libapikey? Sorry for my ignorance and many thanks for your advice.
  • @monika.barget: Just to be sure, you're aware that you can drag multiple items to a tag in the tag selector to assign tags to multiple items?
  • As for pyzotero -- not a tutorial, but the library is very well documented:
  • No, I did not know about the tag selector functions, so thanks a lot for pointing them out to me. This is pretty awesome! :-) I think I will still need scripts to replace tags with recurring punctuation issues, but I could already fix many issues in the standalone. What I also really like is the option to attribute colours to individual tags so that all related items are highlighted in my library. This will really help with my current DH research. I intend to visualise my Zotero data based on publication place and content, so thanks to everyone who has contributed to all the great features.
  • Hi Monika,

    you asked where the libnumber, libtype and libapikey you need for pyzotero come from.

    For libtype there exist only 2 possiblities "user" or "group" - whether which kind of library you will treat. MyLib is of type "user".

    lipapikey you can generate in your account at (settings/feeds-api)

    The libnumber you also find under settings/feeds-api. It's your userID. Thats the libnumber for your "user" type lib. The libnumbers for the groups you can find, if you click in in your account on the corresponding group-library. The number displayed in the url is the libnumber for this group to use for pyzotero.

    That's the way I have found the stuff. Perhaps there exist a more easier way to do it?
Sign In or Register to comment.