@ww_nero/mini-cli 1.0.61 → 1.0.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/config.js +401 -376
- package/src/tools/bash.js +12 -12
- package/src/tools/index.js +13 -4
- package/src/tools/mcp.js +5 -4
- package/src/tools/convert.js +0 -297
- package/src/tools/python/html_to_png.py +0 -100
- package/src/tools/python/html_to_pptx.py +0 -163
- package/src/tools/python/pdf_to_png.py +0 -58
- package/src/tools/python/pptx_to_pdf.py +0 -107
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
"""Convert PDF pages to PNG files."""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import argparse
|
|
5
|
-
import sys
|
|
6
|
-
from pathlib import Path
|
|
7
|
-
from typing import List
|
|
8
|
-
|
|
9
|
-
from pdf2image import convert_from_path
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
def convert_pdf_to_png(input_file: str | Path, output_dir: str | Path, dpi: int = 300, fmt: str = 'png') -> List[Path]:
|
|
13
|
-
"""Convert a PDF into PNG images and return saved file paths."""
|
|
14
|
-
input_path = Path(input_file).expanduser().resolve()
|
|
15
|
-
if not input_path.exists():
|
|
16
|
-
raise FileNotFoundError(f'PDF文件不存在: {input_path}')
|
|
17
|
-
|
|
18
|
-
output_path = Path(output_dir).expanduser().resolve()
|
|
19
|
-
output_path.mkdir(parents=True, exist_ok=True)
|
|
20
|
-
|
|
21
|
-
format_lower = fmt.lower() or 'png'
|
|
22
|
-
images = convert_from_path(str(input_path), dpi=dpi, fmt=format_lower)
|
|
23
|
-
|
|
24
|
-
saved_files: List[Path] = []
|
|
25
|
-
for index, image in enumerate(images, start=1):
|
|
26
|
-
file_name = f'page_{index:04d}.{format_lower}'
|
|
27
|
-
target = output_path / file_name
|
|
28
|
-
image.save(str(target), format_lower.upper())
|
|
29
|
-
saved_files.append(target)
|
|
30
|
-
|
|
31
|
-
return saved_files
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
def _parse_args() -> argparse.Namespace:
|
|
35
|
-
parser = argparse.ArgumentParser(description='将 PDF 转换为 PNG 图片。')
|
|
36
|
-
parser.add_argument('--input', required=True, help='待转换的 PDF 文件路径')
|
|
37
|
-
parser.add_argument('--output-dir', required=True, help='保存图片的目录')
|
|
38
|
-
parser.add_argument('--dpi', type=int, default=300, help='输出图片的 DPI,默认 300')
|
|
39
|
-
parser.add_argument('--format', default='png', help='输出图片格式,默认 PNG')
|
|
40
|
-
return parser.parse_args()
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
def main() -> None:
|
|
44
|
-
args = _parse_args()
|
|
45
|
-
try:
|
|
46
|
-
results = convert_pdf_to_png(args.input, args.output_dir, dpi=args.dpi, fmt=args.format)
|
|
47
|
-
if not results:
|
|
48
|
-
print('未生成任何图片')
|
|
49
|
-
return
|
|
50
|
-
print(f'成功生成 {len(results)} 张图片:')
|
|
51
|
-
for path in results:
|
|
52
|
-
print(path)
|
|
53
|
-
except Exception as error:
|
|
54
|
-
print(f'转换失败: {error}', file=sys.stderr)
|
|
55
|
-
sys.exit(1)
|
|
56
|
-
|
|
57
|
-
if __name__ == '__main__':
|
|
58
|
-
main()
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
"""使用 LibreOffice 无头模式将 PPT/PPTX 直接转换为 PDF。"""
|
|
2
|
-
from __future__ import annotations
|
|
3
|
-
|
|
4
|
-
import argparse
|
|
5
|
-
import os
|
|
6
|
-
import platform
|
|
7
|
-
import shutil
|
|
8
|
-
import subprocess
|
|
9
|
-
import sys
|
|
10
|
-
import tempfile
|
|
11
|
-
from pathlib import Path
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
def _detect_libreoffice() -> str:
|
|
15
|
-
"""寻找 LibreOffice 可执行文件路径。"""
|
|
16
|
-
system = platform.system()
|
|
17
|
-
candidates: list[str] = []
|
|
18
|
-
|
|
19
|
-
if system == 'Darwin':
|
|
20
|
-
candidates.append('/Applications/LibreOffice.app/Contents/MacOS/soffice')
|
|
21
|
-
elif system == 'Windows':
|
|
22
|
-
program_dirs = [
|
|
23
|
-
os.environ.get('PROGRAMFILES', ''),
|
|
24
|
-
os.environ.get('PROGRAMFILES(X86)', '')
|
|
25
|
-
]
|
|
26
|
-
for base in program_dirs:
|
|
27
|
-
if base:
|
|
28
|
-
candidates.append(str(Path(base) / 'LibreOffice' / 'program' / 'soffice.exe'))
|
|
29
|
-
|
|
30
|
-
# 通用命令放在末尾,优先尝试绝对路径
|
|
31
|
-
candidates.extend(['soffice', 'libreoffice'])
|
|
32
|
-
|
|
33
|
-
for candidate in candidates:
|
|
34
|
-
candidate_path = Path(candidate)
|
|
35
|
-
if candidate_path.exists():
|
|
36
|
-
return str(candidate_path)
|
|
37
|
-
|
|
38
|
-
resolved = shutil.which(candidate)
|
|
39
|
-
if resolved:
|
|
40
|
-
return resolved
|
|
41
|
-
|
|
42
|
-
return ''
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
def pptx_to_pdf(input_file: str | Path, output_file: str | Path) -> Path:
|
|
46
|
-
pptx_path = Path(input_file).expanduser().resolve()
|
|
47
|
-
if not pptx_path.exists():
|
|
48
|
-
raise FileNotFoundError(f'PPT/PPTX 文件不存在: {pptx_path}')
|
|
49
|
-
if pptx_path.is_dir():
|
|
50
|
-
raise ValueError(f'输入路径不是文件: {pptx_path}')
|
|
51
|
-
|
|
52
|
-
output_path = Path(output_file).expanduser().resolve()
|
|
53
|
-
output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
54
|
-
|
|
55
|
-
libreoffice_cmd = _detect_libreoffice()
|
|
56
|
-
if not libreoffice_cmd:
|
|
57
|
-
raise RuntimeError('未找到 LibreOffice,请先安装(例如 apt install libreoffice 或从官网安装包)。')
|
|
58
|
-
|
|
59
|
-
with tempfile.TemporaryDirectory(prefix='pptx_to_pdf_') as temp_dir:
|
|
60
|
-
cmd = [
|
|
61
|
-
libreoffice_cmd,
|
|
62
|
-
'--headless',
|
|
63
|
-
'--invisible',
|
|
64
|
-
'--convert-to', 'pdf',
|
|
65
|
-
'--outdir', temp_dir,
|
|
66
|
-
str(pptx_path)
|
|
67
|
-
]
|
|
68
|
-
|
|
69
|
-
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
70
|
-
if result.returncode != 0:
|
|
71
|
-
message = result.stderr.strip() or result.stdout.strip() or 'LibreOffice 返回非零退出码'
|
|
72
|
-
raise RuntimeError(f'LibreOffice 转换失败: {message}')
|
|
73
|
-
|
|
74
|
-
generated = Path(temp_dir) / f'{pptx_path.stem}.pdf'
|
|
75
|
-
if not generated.exists():
|
|
76
|
-
pdf_candidates = list(Path(temp_dir).glob('*.pdf'))
|
|
77
|
-
if len(pdf_candidates) == 1:
|
|
78
|
-
generated = pdf_candidates[0]
|
|
79
|
-
else:
|
|
80
|
-
raise RuntimeError('未找到 LibreOffice 生成的 PDF 文件')
|
|
81
|
-
|
|
82
|
-
if output_path.exists():
|
|
83
|
-
output_path.unlink()
|
|
84
|
-
|
|
85
|
-
shutil.move(str(generated), str(output_path))
|
|
86
|
-
return output_path
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
def _parse_args() -> argparse.Namespace:
|
|
90
|
-
parser = argparse.ArgumentParser(description='将 PPT/PPTX 转换为 PDF(调用 LibreOffice 无头模式)。')
|
|
91
|
-
parser.add_argument('--input', required=True, help='PPT/PPTX 文件路径')
|
|
92
|
-
parser.add_argument('--output', required=True, help='输出 PDF 文件路径')
|
|
93
|
-
return parser.parse_args()
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
def main() -> None:
|
|
97
|
-
args = _parse_args()
|
|
98
|
-
try:
|
|
99
|
-
result = pptx_to_pdf(args.input, args.output)
|
|
100
|
-
print(f'已生成 PDF: {result}')
|
|
101
|
-
except Exception as error:
|
|
102
|
-
print(f'转换失败: {error}', file=sys.stderr)
|
|
103
|
-
sys.exit(1)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if __name__ == '__main__':
|
|
107
|
-
main()
|