class AIFileHandler: def __init__(self, files_path, base_path): self.app = GetActiveObject("Illustrator.Application") self.files_path = files_path self.base_path = base_path def process_file(self, file_name): print(f'Processing file {file_name}') doc = self.app.Open(f'{self.files_path}/{file_name}') layers = doc.Layers print(f'Total layers: {len(layers)}') png_export_options = win32.Dispatch("Illustrator.ExportOptionsPNG24") png_export_options.horizontalScale = 200 png_export_options.verticalScale = 200 image_capture_options = win32.Dispatch("Illustrator.imageCaptureOptions") image_capture_options.resolution = 250 image_capture_options.transparency = True image_capture_options.antiAliasing = True svg_export_options = win32.Dispatch("Illustrator.ExportOptionsSVG") svg_export_options.fontType = 2 svg_export_options.embedRasterImages = True # svg_export_options.cssProperties = 3 svg_export_options.fontSubsetting = 1 # aiNoFonts for layer in layers: layer.visible = False for layer in layers: print(f'Processing layer {layer.name}...') layer.visible = True if len(layer.TextFrames) != 1 or len(layer.GroupItems) != 1: print(f'❗ Fail to export layer {layer.name}') layer.visible = False continue text_frame = layer.TextFrames[0] exported_name = text_frame.contents text_frame.selected = True self.app.ExecuteMenuCommand("hide") text_frame.selected = False group = layer.GroupItems[0] bounds = group.controlBounds group.selected = True # doc.Export(ExportFile=f'{exported_name}.png',ExportFormat=5,Options=png_export_options) doc.ImageCapture(f'{self.base_path}/{exported_name}.png', group.controlBounds, image_capture_options) doc.FitArtboardToSelectedArt(0) doc.Export(ExportFile=f'{self.base_path}/{exported_name}.svg',ExportFormat=3, Options=svg_export_options) group.selected = False layer.visible = False print(f'✓ Succeed to export layer {layer.name}') # break print(f'Exported file {file_name}\r\n')
def send_to_ps(filename): try: app = GetActiveObject("Photoshop.Application") except Exception as e: print( "couldn't find an opened photoshop. Will try to open a new session." ) app = Dispatch("Photoshop.Application") doc = app.Open(filename)
# This script demonstrates how you can use the action manager # to execute the Smart Sharpen filter. from win32com.client import Dispatch, GetActiveObject, GetObject # Start up Photoshop application # Or get Reference to already running Photoshop application instance # app = Dispatch('Photoshop.Application') app = GetActiveObject("Photoshop.Application") fileName = "C:\Git\PS_Samples_Files\Layer Comps.psd" docRef = app.Open(fileName) nLayerSets = docRef.LayerSets nArtLayers = docRef.LayerSets.Item(len(nLayerSets)).ArtLayers docRef.ActiveLayer = docRef.LayerSets.Item(len(nLayerSets)).ArtLayers.Item(len(nArtLayers)) def SmartSharpen(inAmount, inRadius, inNoise): idsmartSharpenID = app.stringIDToTypeID("smartSharpen") desc37 = Dispatch('Photoshop.ActionDescriptor') idpresetKind = app.stringIDToTypeID("presetKind") idpresetKindType = app.stringIDToTypeID("presetKindType") idpresetKindCustom = app.stringIDToTypeID("presetKindCustom") desc37.putEnumerated(idpresetKind, idpresetKindType, idpresetKindCustom) idAmnt = app.charIDToTypeID("Amnt") idPrc = app.charIDToTypeID("#Prc")
# Crop and rotate the active document. from win32com.client import Dispatch, GetActiveObject, GetObject # Start up Photoshop application # Or get Reference to already running Photoshop application instance # app = Dispatch('Photoshop.Application') app = GetActiveObject("Photoshop.Application") # PS constants, see psCC2018.py psPixels = 1 psNewRGB = 2 psWhite = 1 fileName = "C:\Git\photoshop-scripting-python\PS_Samples_Files\Layer Comps.psd" srcDoc = app.Open(fileName) strtRulerUnits = app.Preferences.RulerUnits if strtRulerUnits is not psPixels: app.Preferences.RulerUnits = psPixels # crop a 10 pixel border from the image bounds = [10, 10, srcDoc.Width - 10, srcDoc.Height - 10] srcDoc.RotateCanvas(45) srcDoc.Crop(bounds) # set ruler back to where it was app.Preferences.RulerUnits = strtRulerUnits