Skip to main content
Participant
April 6, 2023
Question

Change the FillColor of a group

  • April 6, 2023
  • 1 reply
  • 411 views
from win32com.client import GetActiveObject, Dispatch, constants
from pathlib import Path


app = GetActiveObject("Illustrator.Application")

docRef = app.ActiveDocument

fpath = Path('generates_test_modified_svg_file.svg').absolute()

current_item = docRef.GroupItems.Item("originalGroup0")
placedSVG = current_item.GroupItems.CreateFromFile(fpath)
placedSVG.Resize(90, 90)
#
placeholder = current_item.PageItems.Item("svgPlaceholder")
placeholder_bounds = placeholder.GeometricBounds
#
# # Set the position of the new object to match the placeholder
placedSVG.Left = placeholder_bounds[0]
placedSVG.Top = placeholder_bounds[1]
#
placedSVG.Name = "barcodeGroup"


current_item = docRef.GroupItems.Item("barcodeGroup")
internal_group = current_item.GroupItems.Item("barcode group")

pantoneSwatchName = "PANTONE Black"

pantoneSwatch = docRef.Swatches.Item(pantoneSwatchName)

rect = current_item.PageItems.Item("barcodeGroup")
original_barcode_group = rect.PageItems.Item("barcode group")

# Loop through all the items in the group
for item in original_barcode_group.PageItems:
    if item.PageItemType == 5:
        item.FillColor = pantoneSwatch.Color
    else:
        print(item.typename)
        new_outlined_item = item.CreateOutline()
        print("------------"+item.typename)
        for compoundPath in new_outlined_item.PageItems:
            for i in range(compoundPath.PathItems.Count):
                compoundPath.PathItems[i].FillColor = pantoneSwatch.Color

I have an SVG file in which I want to change the colour of all its objects into Pantone colour and I have a simple placeholder in the document which I am replacing with the SVG one. After placing the SVG in the document it creates a group.

Now I want to change the FillColor of that group but "rect.FillColor = pantoneSwatch.Color" is not working that's why I try to loop through all of the objects in the group and change its colour but it's taking a long time almost 2 min to change the colour of the group. And there are only 18 objects in the layer. Is there any way to optimize this or set the colour of the group at once? Or while I am placing the SVG maybe change the colour of it at that time. I am kinda stuck here.

 

 

My Group structure:

originalGroup0
  -> barcodeGroup
       -> barcode group
            -> All objects

 

This topic has been closed for replies.

1 reply

CarlosCanto
Community Expert
Community Expert
April 6, 2023

you can't recolor a group at once because groupItems don't have fillColor properties, however changing the default document color applies such color to all selected elements

 

this is how it's done in javascript

docRef.defaultFillColor = pantoneSwatch.color;
Participant
April 6, 2023

If I set the defaultFillColor and then place objects in the document does it automatically change the colour? cause I have only one colour that I need to set in the SVG not multiple color.

CarlosCanto
Community Expert
Community Expert
April 6, 2023

probably not, place your svg first then if the group is selected, change the default fill color, if not, then select your group first before changing the default fill color