|
1 | | -"""Add slate information dynamically. |
| 1 | +"""Demonstrate how to add and update slate information in a Photoshop template. |
2 | 2 |
|
3 | | -- Open template. |
4 | | -- Update info. |
5 | | -- Save as jpg. |
6 | | -- Close current document. |
| 3 | +This example shows how to: |
| 4 | +1. Open a PSD template file |
| 5 | +2. Update text layers with dynamic information |
| 6 | +3. Save the result as a JPG file |
| 7 | +4. Display the saved file |
7 | 8 |
|
| 9 | +The script uses a template PSD file that contains text layers within a layer set |
| 10 | +named "template". It updates specific text layers with dynamic content like |
| 11 | +project name and current date. |
| 12 | +
|
| 13 | +Example: |
| 14 | + ```python |
| 15 | + with Session(template_file, action="open") as ps: |
| 16 | + layer_set = ps.active_document.layerSets.getByName("template") |
| 17 | + for layer in layer_set.layers: |
| 18 | + if layer.kind == ps.LayerKind.TextLayer: |
| 19 | + layer.textItem.contents = dynamic_data[layer.textItem.contents] |
| 20 | + ``` |
| 21 | +
|
| 22 | +Note: |
| 23 | + - Requires a PSD template file with text layers in a layer set named "template" |
| 24 | + - Text layers should have placeholder text matching keys in the data dictionary |
| 25 | + - The script uses os.startfile which is Windows-specific |
8 | 26 | """ |
9 | 27 |
|
10 | 28 | # Import built-in modules |
11 | 29 | from __future__ import annotations |
12 | 30 |
|
13 | 31 | import os |
14 | | -from datetime import datetime |
15 | | -from tempfile import mkdtemp |
| 32 | +from datetime import datetime, timezone |
16 | 33 | from pathlib import Path |
17 | | -from datetime import timezone |
| 34 | +from tempfile import mkdtemp |
18 | 35 |
|
19 | 36 | # Import third-party modules |
20 | 37 | import examples._psd_files as psd # Import from examples. |
21 | 38 |
|
22 | 39 | # Import local modules |
23 | 40 | from photoshop import Session |
24 | 41 |
|
| 42 | +# Get path to template PSD file |
25 | 43 | PSD_FILE = psd.get_psd_files() |
26 | 44 | slate_template = PSD_FILE["slate_template.psd"] |
| 45 | + |
| 46 | +# Open template file in Photoshop |
27 | 47 | with Session(slate_template, action="open", auto_close=True) as ps: |
| 48 | + # Get the layer set named "template" |
28 | 49 | layer_set = ps.active_document.layerSets.getByName("template") |
29 | 50 |
|
| 51 | + # Prepare dynamic data for text layers |
30 | 52 | data = { |
31 | 53 | "project name": "test_project", |
32 | 54 | "datetime": datetime.now(tz=timezone.utc).strftime("%Y-%m-%d"), |
33 | 55 | } |
| 56 | + |
| 57 | + # Update text layers with dynamic content |
34 | 58 | for layer in layer_set.layers: |
35 | 59 | if layer.kind == ps.LayerKind.TextLayer: |
36 | 60 | layer.textItem.contents = data[layer.textItem.contents.strip()] |
37 | 61 |
|
| 62 | + # Save the document as JPG in a temporary directory |
38 | 63 | jpg_file = Path(mkdtemp("photoshop-python-api")) / "slate.jpg" |
39 | 64 | ps.active_document.saveAs(str(jpg_file), ps.JPEGSaveOptions()) |
40 | 65 | ps.echo(f"Save jpg to {jpg_file}") |
| 66 | + |
| 67 | + # Open the saved JPG file (Windows-specific) |
41 | 68 | # Note: os.startfile is Windows-specific, consider using a cross-platform solution |
42 | 69 | os.startfile(str(jpg_file)) |
0 commit comments