From 121c165d624271d5294bf0dca75584b0d0ca3c30 Mon Sep 17 00:00:00 2001 From: Sidney Zhang Date: Wed, 11 Feb 2026 18:33:30 +0800 Subject: [PATCH] 20260211 --- src/pptopic/main.py | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/pptopic/main.py b/src/pptopic/main.py index c514718..4c982b5 100644 --- a/src/pptopic/main.py +++ b/src/pptopic/main.py @@ -28,17 +28,20 @@ def export( pptopic export presentation.pptx --output result.png --optimize --max-height 20000 """ try: + if output_name: + save_name = f"{output_name}.{format.lower()}" if "." not in output_name else output_name + else: + save_name = f"{input_file.stem}_{today(fmt="YYYYMMDDHHmm",addtime=True)}.{format.lower()}" + PPT_longPic( pptFile=input_file, - saveName=f"{output_name}.{format.lower()}" if output_name and "." not in output_name else output_name, + saveName=save_name, width=width, saveto=output_dir ) if optimize: - output_path = output_dir / (output_name if output_name else input_file.stem) - if not output_path.suffix: - output_path = output_path.with_suffix(f".{format.lower()}") + output_path = output_dir / save_name imageOptimization( imageFile=output_path, @@ -48,10 +51,42 @@ def export( engine_conf=engine_conf ) - typer.echo(f"成功导出长图: {output_dir / (output_name if output_name else input_file.stem)}") + typer.echo(f"成功导出长图: {output_dir / save_name}") except Exception as e: typer.echo(f"导出失败: {e}", err=True) raise typer.Exit(code=1) +@app.command() +def optimize( + input_file: Path = typer.Argument(..., help="输入的图片文件路径", exists=True), + output_file: Path = typer.Option(None, "--output", "-o", help="输出图片文件路径,默认覆盖原文件"), + max_height: int = typer.Option(29999, "--max-height", help="优化图片的最大高度(默认29999)"), + engine: str = typer.Option("pngquant", "--engine", help="图片优化引擎(默认pngquant)"), + engine_conf: str = typer.Option("--skip-if-larger", "--engine-conf", help="图片优化引擎配置参数(默认--skip-if-larger)") +): + """ + 单独优化图片 + + 示例: + pptopic optimize image.png + pptopic optimize image.png --output optimized.png --max-height 20000 + pptopic optimize image.png --engine pngquant --engine-conf "--quality=80-90" + """ + try: + save_file = output_file if output_file else input_file + + imageOptimization( + imageFile=input_file, + saveFile=save_file, + max_height=max_height, + engine=engine, + engine_conf=engine_conf + ) + + typer.echo(f"成功优化图片: {save_file}") + except Exception as e: + typer.echo(f"优化失败: {e}", err=True) + raise typer.Exit(code=1) + def main(): app()