Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Quick modification of find replace to allow multiple replacement at once with comma separated lists from a spreadsheet/csv.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Chris Mohler
# Multi-replace: Eyecreate
# Copyright 2008 Chris Mohler
# License: GPL v3
# GIMP plugin to perform search and replace on all text layers
from gimpfu import *
import re
gettext.install("gimp20-python", gimp.locale_directory, unicode=True)
def find_and_replace(image, drawable, find, replace, all, case):
layers = image.layers #get image layers
if all: count = 0 #replace all
else: count = 1 #replace only first match
splitFind = find.split(',')
splitReplace = replace.split(',')
splitReplaceCount = 0
for element in splitFind:
if not case: element = re.compile(element, re.I) #case-insentive
for layer in layers:
if pdb.gimp_drawable_is_text_layer(layer): #only act on text layer
haystack = pdb.gimp_text_layer_get_text(layer) # get texr
if haystack == element: #prevents replacing parts of strings
replaced = re.sub(element, splitReplace[splitReplaceCount], haystack, count) # replace text
pdb.gimp_text_layer_set_text(layer, str(replaced)) # set text
splitReplaceCount += 1
"Find and Replace text in all text layers.",
"Find and Replace text in all text layers.",
"Chris Mohler",
"Chris Mohler",
"<Image>/Edit/Find & Replace Text...",
"RGB*, GRAY*",
(PF_STRING, "find", "Find (comma seperated):", ""),
(PF_STRING, "replace", "Replace (comma seperated):", ""),
(PF_TOGGLE, "all", "Replace All?", True ),
(PF_TOGGLE, "case", "Case Sensitive?", True),
domain=("gimp20-python", gimp.locale_directory))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment