Python script for reading a PSD template and applying contents in a spreadsheet to export plenty of images. It's an alternative to Photoshop Image > Variables > Define.
📺 Example - Create spreadsheet
create_xlsx.mp4
📺 Example - Manual export
batch_export.mp4
📺 Example - Auto export
auto_export.mp4
This is what you do using Photoshop:
- Edit content in a spreadsheet.
- Save spreadsheet as a CSV file.
- Define variables for layers in Photoshop.
- Import the CSV file.
- Export data sets as files. Unfortunately, you get .psd files.
- Make a automate-batch to save PSD as JPG or PNG.
- Use the automate-batch to get final images.
Here's what you do using my Python script.
- Edit content in a spreadsheet.
- Run psd_renderer.py
That's it. Images will be just there for you. All you need is a Python environment with a few packages.
Two ways of downloading:
git clone https://github.com/greenzorro/excel-ps-batch-export.git- click the button Code, then Download ZIP
For the first time, you'll need some basic setup:
- Place your PSD template files here.
- Edit PSD template file. Rename all changeable layers or groups with this pattern:
- Format:
@Variable_name#Operation_Parameter - A layer name may be like:
@badge#v,@description#t_por@bg#i @tells the script that this layer is changeable, go and get content from the spreadsheetVariable_nameshould appear in the spreadsheet as column head#Operation_Parametertells the script what to do with the layer#vto set visibility according to TRUE/FALSE in the spreadsheet#tto replace a text layer content with spreadsheet data, parameters including:- Text align left top by default
_cfor horizontally center alignment_rfor horizontally right alignment_pfor paragraph with text wraping, fill the paragraph text layer in PSD with at least one line_pmfor vertically middle alignment_pbfor vertically bottom alignment- All these parameters work together, like
#t_c_p,#t_r_pb - Alignment set in PSD will not affect the result, the program only checks layer names
#ito fill a pixel layer with the image whose file path is written in the spreadsheet- One thing to note: Do not use cmd/ctrl+T to scale changeable text layers. Adjust their sizes only via font size attribute, otherwise the script will get wrong text sizes from the PSD file. If you already did, make new text layers to replace them.
- Format:
- Run
xlsx_generator.py. Your XLSX files will appear, with columns ready. - Edit XLSX file. Python reads the first sheet, put your data there. Or you may follow the example, put your data in another sheet and use Excel formulas in the first one to read and calculate everything. It's especially useful when you want to toggle layer visibility. DO NOT delete the first
File_namecolumn, leave it blank to use the default file name format(image_1, image_2, etc). - Put everything else the templates need in
assetsfolder, including fonts, background images, etc. Make sure the path to image assets match the data in the spreadsheet.
Looks complicated huh? Trust me, it's way more complicated doing the same thing using Photoshop. And once you've done setting up, this would be your life saver.
When it comes to exporting. Things become a piece of cake:
- Paste content in the spreadsheet.
- Run psd_renderer.py
I even made another script (file_monitor.py) to moniter the spreadsheet and export images automatically once the spreadsheets are modified.
This tool supports processing multiple PSD templates with one Excel file. Here's how it works:
- Grouping by Prefix: All PSD files in the same directory are grouped by their prefix. The prefix is defined as the part of the filename before the first hash (
#). For example:product_intro#templateA.psdandproduct_intro#templateB.psdshare the same prefixproduct_intro
- Shared Excel: For each group, a single Excel file (named
[prefix].xlsx) is created. This Excel file contains variables from all PSDs in the group. - Batch Export: When running
psd_renderer.py [prefix] ..., the script will process all PSDs in the group. Each row in the Excel will generate one image for every PSD in the group. The output image filenames include the PSD's suffix (e.g.,row1_templateA.jpg).
Example:
- PSD files:
campaign#summer.psd,campaign#winter.psd - Excel file:
campaign.xlsx - Command:
python psd_renderer.py campaign AlibabaPuHuiTi-2-85-Bold.ttf jpg - Output: For each row in
campaign.xlsx, two images are generated:row1_summer.jpg,row1_winter.jpg, etc.
It's recommended to use the requirements.txt file to install all dependencies:
pip install pillow pandas openpyxl psd-tools tqdmThe project includes a comprehensive test suite to ensure code quality and functionality:
# Run all tests (recommended)
python test/run_tests.py all
# Run specific test file
python -m pytest test/test_simple.py -v
# Generate coverage report
python test/run_tests.py coverageTest Coverage: 67 tests covering core functionality, business logic, error handling, boundary conditions, and performance scenarios with strict validation standards.
# Basic command format
python psd_renderer.py [Excel_file_prefix] [font_file] [output_format]
# Example
python psd_renderer.py 1 AlibabaPuHuiTi-2-85-Bold.ttf jpgSpecial thanks to psd-tools for providing such powerful APIs to interact with PSD files. Therefore I could ultilize the power of Photoshop at image editing and Excel/Python at data processing.
Created by Victor_42