Skip to content

Instantly share code, notes, and snippets.

@aquaresima
Created June 19, 2023 11:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aquaresima/7e3d7ef805cdadc4d548d773940d9ead to your computer and use it in GitHub Desktop.
Save aquaresima/7e3d7ef805cdadc4d548d773940d9ead to your computer and use it in GitHub Desktop.
Script to make all zotero tags lowercase
# The script below will update ALL your tags except those in the skip_tags list. Use it with caution.
# It also splits all tags that have more than a word. It saves both the old tag and the new one.
# That is, you have ("my tag word"), and you get ("my","tag", "word", "my tag word").
#The API_KEY and LIBRARY_ID are private. See the documentation.api_key = "xxxxx"
# Documentation: https://pyzotero.readthedocs.io/en/latest/
# Original thread: https://forums.zotero.org/discussion/92493/any-way-to-switch-all-tags-to-all-lower-case-or-ignore-case-in-tags
library_id = 0000000
library_type = "user"
from pyzotero import zotero
zot = zotero.Zotero(library_id, library_type, api_key)
skip_tags = [
"⛔ No doifound",
"⛔ No DOI found",
"#broken_attachments",
"#duplicate_attachments",
"#nosource",
"❓ multiple doi"
]
collections_keys = [x for x in zot.collections()]
for coll in zot.collections():
# if coll["data"]["name"] != "TEST_COLLECTION":
# continue
print("UPDATING: collection: " + coll["data"]["name"] )
print(coll["key"])
print(zot.num_collectionitems(coll["key"]))
items = zot.collection_items(coll["key"])
update_items = []
for item in items:
old_tags = item["data"]["tags"].copy()
for tag in item["data"]["tags"]:
if tag["tag"] in skip_tags: # skip tags
continue
if tag["tag"].startswith("zotero"): # it belongs to BetterNotes
continue
tag["tag"] = tag["tag"].lower()
multiple_tags = tag["tag"].split(" ")
if len(multiple_tags) > 1:
for new_tag in multiple_tags:
item["data"]["tags"].append({"tag":new_tag})
new_tags = item["data"]["tags"]
if old_tags != new_tags:
changed_tags = [x["tag"].lower() for x in item["data"]["tags"]]
print(changed_tags)
zot.update_item(item)
# for item in items:
# # print(item["data"]["title"])
# print(item["data"]["tags"])
print("DONE")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment