Files
pptopic/src/pptopic/main.py
Sidney Zhang 97b30ace84 feat(lib): 添加批量图片优化功能与实时进度显示
- 新增 `batchImageOptimization` 函数支持批量处理图片
- 为图片优化添加 spinner 进度指示器和实时计时显示
- 更新版本号至 0.3.2
2026-06-18 16:17:38 +08:00

139 lines
5.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from pathlib import Path
import typer
from simtoolsz.utils import today
from pptopic.lib import PPT_longPic, batchImageOptimization, imageOptimization
app = typer.Typer()
@app.command()
def export(
input_file: Path = typer.Argument(..., help="输入的PPTX文件路径", exists=True),
output_name: str = typer.Option(None, "--output", "-o", help="输出文件名(不含扩展名或含扩展名)"),
output_dir: Path = typer.Option(".", "--dir", "-d", help="输出目录,默认为当前目录"),
width: int = typer.Option(None, "--width", "-w", help="导出图片的宽度(像素)"),
optimize: bool = typer.Option(False, "--optimize", 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"
),
format: str = typer.Option("PNG", "--format", "-f", help="输出格式JPG或PNG默认PNG"),
):
"""
将PPTX文件导出为长图
示例:
pptopic export presentation.pptx
pptopic export presentation.pptx --optimize
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=save_name, width=width, saveto=output_dir)
if optimize:
output_path = output_dir / save_name
imageOptimization(
imageFile=output_path,
saveFile=output_path,
max_height=max_height,
engine=engine,
engine_conf=engine_conf,
)
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)
@app.command()
def batch_optimize(
input_dir: Path = typer.Argument(..., help="输入图片目录", exists=True, file_okay=False),
output_dir: Path = typer.Option(None, "--output", "-o", help="输出目录,默认覆盖原文件"),
pattern: str = typer.Option("*.png", "--pattern", "-p", help="文件匹配模式(默认 *.png"),
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 batch-optimize ./images
pptopic batch-optimize ./images --pattern "*.jpg" --output ./optimized
pptopic batch-optimize ./images --max-height 20000 --engine pngquant
"""
try:
image_files = sorted(input_dir.glob(pattern))
if not image_files:
typer.echo(f"{input_dir} 中没有找到匹配 {pattern} 的文件", err=True)
raise typer.Exit(code=1)
results = batchImageOptimization(
image_files=image_files,
save_dir=output_dir,
max_height=max_height,
engine=engine,
engine_conf=engine_conf,
)
typer.echo(f"成功优化 {len(results)}/{len(image_files)} 张图片")
except Exception as e:
typer.echo(f"批量优化失败: {e}", err=True)
raise typer.Exit(code=1)
@app.command()
def version():
"""显示版本信息"""
from pptopic import __version__
typer.echo(f"pptopic version: {__version__}")
typer.echo("Under the MIT License.")
def main():
app()