63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
import argparse
|
|
import logging
|
|
import os
|
|
|
|
from lib import split_multipage_svg
|
|
|
|
logger = logging.getLogger(__name__)
|
|
handler = logging.StreamHandler()
|
|
formatter = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")
|
|
handler.setFormatter(formatter)
|
|
logger.addHandler(handler)
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="Split a multi-page SVG into individual page SVGs"
|
|
)
|
|
parser.add_argument("--input", "-i", required=True, help="Path to input SVG file")
|
|
parser.add_argument(
|
|
"--output", "-o", required=True, help="Path to output directory for SVG files"
|
|
)
|
|
parser.add_argument("--log-level", default="INFO", help="Logging level")
|
|
|
|
args = parser.parse_args()
|
|
logger.setLevel(args.log_level)
|
|
|
|
logger.info(f"Reading SVG from {args.input}")
|
|
try:
|
|
with open(args.input, "r") as f:
|
|
input_svg = f.read()
|
|
except Exception as e:
|
|
logger.error(f"Error reading input file: {e}")
|
|
return 1
|
|
|
|
logger.info("Splitting SVG.")
|
|
split_svg_contents = split_multipage_svg(input_svg)
|
|
|
|
output_directory = args.output
|
|
logger.info(f"Writing split SVG files to {output_directory}")
|
|
try:
|
|
if not os.path.exists(output_directory):
|
|
os.makedirs(output_directory)
|
|
|
|
try:
|
|
for i, svg in enumerate(split_svg_contents):
|
|
output_path = os.path.join(output_directory, f"page-{i + 1}.svg")
|
|
with open(output_path, "w") as f:
|
|
f.write(svg)
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error writing output file: {e}")
|
|
return 1
|
|
except Exception as e:
|
|
logger.error(f"Error writing output file: {e}")
|
|
return 1
|
|
|
|
logger.info("SVG splitting complete")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|