Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created January 11, 2026 20:29
Show Gist options
  • Select an option

  • Save jhyland87/385045cf7fdfa49f7b9650fcc63fc44b to your computer and use it in GitHub Desktop.

Select an option

Save jhyland87/385045cf7fdfa49f7b9650fcc63fc44b to your computer and use it in GitHub Desktop.
Replace KiCad symbol font sizes
#!/usr/bin/env python3
"""
Script to replace font sizes in KiCad .kicad_sym files.
Specifically replaces instances where font size is set to 1.27 with a new value.
Usage:
python replace_font_size.py <input_file> <new_size> [output_file]
If output_file is not specified, the input file will be modified in place.
Example:
python replace_font_size.py 4xxx_IEEE.kicad_sym 1.5
python replace_font_size.py 4xxx_IEEE.kicad_sym 1.5 output.kicad_sym
"""
import sys
import re
import os
def replace_font_size(input_file, new_size, output_file=None):
"""
Replace font size 1.27 with new_size in a KiCad symbol file.
Only modifies font size entries, nothing else.
Args:
input_file: Path to input .kicad_sym file
new_size: New font size value (float or string)
output_file: Optional output file path. If None, modifies input_file in place.
"""
try:
# Validate input file exists
if not os.path.isfile(input_file):
print(f"Error: Input file '{input_file}' does not exist.", file=sys.stderr)
sys.exit(1)
# Validate new size is a number
try:
new_size_float = float(new_size)
if new_size_float <= 0:
raise ValueError("Font size must be positive")
except ValueError:
print(f"Error: '{new_size}' is not a valid positive number.", file=sys.stderr)
sys.exit(1)
# Read the file
with open(input_file, 'r', encoding='utf-8') as f:
content = f.read()
# Pattern to match (size 1.27 1.27) - matches exactly 1.27 for both values
# This ensures we only replace font sizes, not other numeric values
pattern = r'\(size\s+1\.27\s+1\.27\)'
# Replace with new size (both X and Y are set to the new value)
replacement = f'(size {new_size} {new_size})'
# Count matches before replacement
matches = len(re.findall(pattern, content))
if matches == 0:
print(f"No font size entries with value 1.27 found in '{input_file}'.")
return
# Perform replacement
new_content = re.sub(pattern, replacement, content)
# Determine output file
if output_file is None:
output_file = input_file
# Write the modified content
with open(output_file, 'w', encoding='utf-8') as f:
f.write(new_content)
print(f"Successfully replaced {matches} font size entries from 1.27 to {new_size} in '{output_file}'.")
except PermissionError:
print(f"Error: Permission denied when accessing '{input_file}'.", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)
def main():
if len(sys.argv) < 3:
print(__doc__, file=sys.stderr)
sys.exit(1)
input_file = sys.argv[1]
new_size = sys.argv[2]
output_file = sys.argv[3] if len(sys.argv) > 3 else None
replace_font_size(input_file, new_size, output_file)
if __name__ == '__main__':
main()
#!/bin/bash
#
# Script to replace font sizes in KiCad .kicad_sym files.
# Specifically replaces instances where font size is set to 1.27 with a new value.
#
# Usage:
# ./replace_font_size.sh <input_file> <new_size> [output_file]
#
# If output_file is not specified, the input file will be modified in place.
#
# Example:
# ./replace_font_size.sh 4xxx_IEEE.kicad_sym 1.5
# ./replace_font_size.sh 4xxx_IEEE.kicad_sym 1.5 output.kicad_sym
set -e
# Check arguments
if [ $# -lt 2 ]; then
echo "Usage: $0 <input_file> <new_size> [output_file]" >&2
echo "" >&2
echo "Replaces font size 1.27 with new_size in a KiCad symbol file." >&2
echo "Only modifies font size entries, nothing else." >&2
exit 1
fi
INPUT_FILE="$1"
NEW_SIZE="$2"
OUTPUT_FILE="${3:-$INPUT_FILE}"
# Validate input file exists
if [ ! -f "$INPUT_FILE" ]; then
echo "Error: Input file '$INPUT_FILE' does not exist." >&2
exit 1
fi
# Validate new_size is a positive number
if ! [[ "$NEW_SIZE" =~ ^[0-9]*\.?[0-9]+$ ]]; then
echo "Error: '$NEW_SIZE' is not a valid number." >&2
exit 1
fi
# Check if it's positive using awk (available on all Unix-like systems)
if ! awk "BEGIN {exit !($NEW_SIZE > 0)}"; then
echo "Error: Font size must be positive." >&2
exit 1
fi
# Count matches before replacement
MATCH_COUNT=$(grep -o '(size 1.27 1.27)' "$INPUT_FILE" | wc -l | tr -d ' ')
if [ "$MATCH_COUNT" -eq 0 ]; then
echo "No font size entries with value 1.27 found in '$INPUT_FILE'."
exit 0
fi
# Perform replacement using sed
# Pattern: (size 1.27 1.27) -> (size NEW_SIZE NEW_SIZE)
# Using a temporary file for in-place editing
if [ "$OUTPUT_FILE" = "$INPUT_FILE" ]; then
# In-place modification
TEMP_FILE=$(mktemp)
sed "s/(size 1\.27 1\.27)/(size $NEW_SIZE $NEW_SIZE)/g" "$INPUT_FILE" > "$TEMP_FILE"
mv "$TEMP_FILE" "$INPUT_FILE"
else
# Write to output file
sed "s/(size 1\.27 1\.27)/(size $NEW_SIZE $NEW_SIZE)/g" "$INPUT_FILE" > "$OUTPUT_FILE"
fi
echo "Successfully replaced $MATCH_COUNT font size entries from 1.27 to $NEW_SIZE in '$OUTPUT_FILE'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment