This Python script is designed to optimize and modify SVG files for use in React applications. It adjusts SVG attributes to make them compatible with React components and ensures a consistent appearance by using the currentColor
property. Additionally, the script cleans up file names by removing spaces and capitalizing each word for improved organization.
-
Dynamic Color Handling:
- Replaces all
fill
andstroke
attributes in SVG files withcurrentColor
to allow color inheritance from parent components in React.
- Replaces all
-
File Renaming:
- Removes spaces in file names and capitalizes each word for a cleaner naming convention.
- Example:
my icon.svg
becomesMyIcon.svg
.
-
Batch Processing:
- Processes all SVG files in the specified folder.
- Skips files without changes and logs the result.
- Python 3.x
-
Clone this repository or download the script file
svg_modifier_for_react.py
. -
Place the script in the same directory as your SVG files or specify a custom folder path.
-
Run the script:
python svg_modifier_for_react.py
- The script will process all SVG files in the specified directory, applying the changes and renaming files if needed.
By default, the script processes SVG files in the current directory (./
). To specify a custom folder path, modify the file_path
variable in the script:
file_path = '/path/to/your/svg/files'
- File:
example icon.svg
- Content:
<svg fill="#000000" stroke="#FF5733"> </svg>
- Renamed File:
ExampleIcon.svg
- Modified Content:
<svg fill="currentColor" stroke="currentColor"> </svg>
The script logs its operations:
- Files modified:
"Changes made to the file: \"ExampleIcon.svg\""
- Files without changes:
"No changes made to the file: \"ExampleIcon.svg\""
- Files renamed:
"Renamed: \"example icon.svg\" -> \"ExampleIcon.svg\""
- Ensure your SVG files are backed up before running the script.
- The script is case-insensitive when checking file extensions (e.g.,
.SVG
is treated the same as.svg
).
This project is licensed under the MIT License. Feel free to use, modify, and distribute this script.
For questions or contributions, feel free to open an issue or submit a pull request!