ms-toolbox 0.1.1__tar.gz

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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MatsubaraSoda
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.4
2
+ Name: ms-toolbox
3
+ Version: 0.1.1
4
+ Summary: MatsubaraSoda 个人工具箱
5
+ Author: MatsubaraSoda
6
+ Author-email: MatsubaraSoda <matsubarasoda@gmail.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Requires-Dist: pypdf>=6.21.1
10
+ Requires-Python: >=3.12
11
+ Project-URL: Homepage, https://github.com/MatsubaraSoda/ms-toolbox
12
+ Project-URL: Issues, https://github.com/MatsubaraSoda/ms-toolbox/issues
13
+ Description-Content-Type: text/markdown
14
+
15
+ # ms-toolbox
16
+
17
+ MatsubaraSoda 个人工具箱
18
+
19
+ ## 安装
20
+
21
+ 需要 Python 3.12+。推荐使用 [uv](https://docs.astral.sh/uv/) 安装为全局命令行工具:
22
+
23
+ ```bash
24
+ # 从 PyPI 安装(推荐)
25
+ uv tool install ms-toolbox
26
+ ```
27
+
28
+ ## 工具列表
29
+
30
+ 安装 ms-toolbox 后,下列命令均可直接使用;完整参数以各命令的 `--help` 输出为准。
31
+
32
+ ### pdf-extract — 提取 PDF 的指定页码范围,另存为新文件
33
+
34
+ ```bash
35
+ pdf-extract -i input.pdf -o output.pdf --start 5 --end 10
36
+ ```
37
+
38
+ 把 `input.pdf` 的第 5 页到第 10 页(含首尾)保存为 `output.pdf`,成功时退出码为 0。
39
+
40
+ | 参数 | 必填 | 说明 |
41
+ | --- | --- | --- |
42
+ | `-i`, `--input` | 是 | 输入 PDF 文件路径 |
43
+ | `-o`, `--output` | 是 | 输出 PDF 文件路径 |
44
+ | `--start` | 是 | 起始页码(1 起始,包含该页) |
45
+ | `--end` | 是 | 结束页码(包含该页) |
46
+
47
+ 退出码:`0` 成功;`1` 提取失败;`2` 意外错误。
48
+
49
+ ```text
50
+ --- 正在启动 PDF 提取工具 ---
51
+ 输入文件:input.pdf
52
+ 输出文件:output.pdf
53
+ 页码范围:5 到 10
54
+ 成功提取第 5 页到第 10 页,并保存到 'output.pdf'。
55
+ ```
56
+
57
+ ## 本地开发
58
+
59
+ ```bash
60
+ git clone https://github.com/MatsubaraSoda/ms-toolbox
61
+ cd ms-toolbox
62
+ uv sync # 创建虚拟环境并安装依赖(含开发依赖)
63
+ uv run pytest -v # 运行测试
64
+ ```
65
+
66
+ ## 许可证
67
+
68
+ 本项目基于 [MIT License](LICENSE) 发布。
@@ -0,0 +1,54 @@
1
+ # ms-toolbox
2
+
3
+ MatsubaraSoda 个人工具箱
4
+
5
+ ## 安装
6
+
7
+ 需要 Python 3.12+。推荐使用 [uv](https://docs.astral.sh/uv/) 安装为全局命令行工具:
8
+
9
+ ```bash
10
+ # 从 PyPI 安装(推荐)
11
+ uv tool install ms-toolbox
12
+ ```
13
+
14
+ ## 工具列表
15
+
16
+ 安装 ms-toolbox 后,下列命令均可直接使用;完整参数以各命令的 `--help` 输出为准。
17
+
18
+ ### pdf-extract — 提取 PDF 的指定页码范围,另存为新文件
19
+
20
+ ```bash
21
+ pdf-extract -i input.pdf -o output.pdf --start 5 --end 10
22
+ ```
23
+
24
+ 把 `input.pdf` 的第 5 页到第 10 页(含首尾)保存为 `output.pdf`,成功时退出码为 0。
25
+
26
+ | 参数 | 必填 | 说明 |
27
+ | --- | --- | --- |
28
+ | `-i`, `--input` | 是 | 输入 PDF 文件路径 |
29
+ | `-o`, `--output` | 是 | 输出 PDF 文件路径 |
30
+ | `--start` | 是 | 起始页码(1 起始,包含该页) |
31
+ | `--end` | 是 | 结束页码(包含该页) |
32
+
33
+ 退出码:`0` 成功;`1` 提取失败;`2` 意外错误。
34
+
35
+ ```text
36
+ --- 正在启动 PDF 提取工具 ---
37
+ 输入文件:input.pdf
38
+ 输出文件:output.pdf
39
+ 页码范围:5 到 10
40
+ 成功提取第 5 页到第 10 页,并保存到 'output.pdf'。
41
+ ```
42
+
43
+ ## 本地开发
44
+
45
+ ```bash
46
+ git clone https://github.com/MatsubaraSoda/ms-toolbox
47
+ cd ms-toolbox
48
+ uv sync # 创建虚拟环境并安装依赖(含开发依赖)
49
+ uv run pytest -v # 运行测试
50
+ ```
51
+
52
+ ## 许可证
53
+
54
+ 本项目基于 [MIT License](LICENSE) 发布。
@@ -0,0 +1,27 @@
1
+ [project]
2
+ name = "ms-toolbox"
3
+ version = "0.1.1"
4
+ description = "MatsubaraSoda 个人工具箱"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ requires-python = ">=3.12"
9
+ dependencies = ["pypdf>=6.21.1"]
10
+
11
+ [[project.authors]]
12
+ name = "MatsubaraSoda"
13
+ email = "matsubarasoda@gmail.com"
14
+
15
+ [project.scripts]
16
+ pdf-extract = "ms_toolbox.pdf.main:main"
17
+
18
+ [project.urls]
19
+ Homepage = "https://github.com/MatsubaraSoda/ms-toolbox"
20
+ Issues = "https://github.com/MatsubaraSoda/ms-toolbox/issues"
21
+
22
+ [dependency-groups]
23
+ dev = ["pytest>=9.1.1"]
24
+
25
+ [build-system]
26
+ requires = ["uv_build>=0.12.6,<0.13.0"]
27
+ build-backend = "uv_build"
@@ -0,0 +1,30 @@
1
+ [project]
2
+ name = "ms-toolbox"
3
+ version = "0.1.1"
4
+ description = "MatsubaraSoda 个人工具箱"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ authors = [
9
+ { name = "MatsubaraSoda", email = "matsubarasoda@gmail.com" }
10
+ ]
11
+ requires-python = ">=3.12"
12
+ dependencies = [
13
+ "pypdf>=6.21.1",
14
+ ]
15
+
16
+ [dependency-groups]
17
+ dev = [
18
+ "pytest>=9.1.1",
19
+ ]
20
+
21
+ [project.scripts]
22
+ pdf-extract = "ms_toolbox.pdf.main:main"
23
+
24
+ [project.urls]
25
+ Homepage = "https://github.com/MatsubaraSoda/ms-toolbox"
26
+ Issues = "https://github.com/MatsubaraSoda/ms-toolbox/issues"
27
+
28
+ [build-system]
29
+ requires = ["uv_build>=0.12.6,<0.13.0"]
30
+ build-backend = "uv_build"
File without changes
File without changes
@@ -0,0 +1,57 @@
1
+ from pathlib import Path # 使用 pathlib 处理路径,确保跨平台兼容性;能够自动处理 `/` 和 `\` 的差异。
2
+ from pypdf import PdfReader, PdfWriter
3
+ from typing import Optional
4
+
5
+ # =================================================
6
+ # 核心业务模块
7
+ # 以函数方式提供各种功能
8
+ # =================================================
9
+
10
+ def get_total_pages(file_path: Path) -> Optional[int]:
11
+ """
12
+ 获取指定 PDF 文件的总页数。
13
+
14
+ Args:
15
+ file_path: PDF 文件的路径,必须是 pathlib.Path 对象。
16
+
17
+ Returns:
18
+ 成功时返回 PDF 的总页数(int);读取失败时返回 None。
19
+ """
20
+ try:
21
+ reader = PdfReader(file_path) # PdfReader 负责打开和读取 PDF 文件
22
+ total_pages = len(reader.pages) # 返回 PDF 的总页数
23
+ return total_pages
24
+
25
+ except Exception as e:
26
+ print(f"处理 PDF 文件 '{file_path.name}' 时发生错误:{e}") # 捕获文件损坏、非 PDF 格式等运行时错误
27
+ return None
28
+
29
+ def extract_and_save_pages(input_file_path: Path, output_file_path: Path, start_page: int, end_page: int) -> bool:
30
+ """
31
+ 提取 PDF 的指定范围,从开始页码到结束页码(包含首尾)。
32
+
33
+ Args:
34
+ input_file_path: 目标 PDF
35
+ output_file_path: 提取后的 PDF
36
+ start_page: 开始页码(1-indexed)
37
+ end_page: 结束页码(1-indexed)
38
+
39
+ Returns:
40
+ 如果成功返回 True,否则返回 False。
41
+ """
42
+ try:
43
+ reader = PdfReader(input_file_path)
44
+ writer = PdfWriter()
45
+
46
+ for page_num in range(start_page - 1, end_page):
47
+ writer.add_page(reader.pages[page_num])
48
+
49
+ with open(output_file_path, "wb") as output_pdf:
50
+ writer.write(output_pdf)
51
+
52
+ print(f"成功提取第 {start_page} 页到第 {end_page} 页,并保存到 '{output_file_path}'。")
53
+ return True
54
+
55
+ except Exception as e:
56
+ print(f"处理 PDF 文件 '{input_file_path.name}' 时发生错误:{e}")
57
+ return False
@@ -0,0 +1,64 @@
1
+ from argparse import ArgumentParser
2
+ from pathlib import Path
3
+ from ms_toolbox.pdf.core import extract_and_save_pages
4
+ import sys
5
+
6
+ def main():
7
+ """
8
+ 命令行主函数:解析参数并调用 PDF 提取的核心逻辑。
9
+ """
10
+ parser = ArgumentParser()
11
+
12
+ parser.add_argument(
13
+ '-i', '--input',
14
+ type=str,
15
+ required=True,
16
+ help='输入 PDF 路径 (e.g., input.pdf)'
17
+ )
18
+ parser.add_argument(
19
+ '-o', '--output',
20
+ type=str,
21
+ required=True,
22
+ help='输出 PDF 路径(e.g., output.pdf)'
23
+ )
24
+ parser.add_argument(
25
+ '--start',
26
+ type=int,
27
+ required=True,
28
+ help='开始页码 (1-indexed)'
29
+ )
30
+ parser.add_argument(
31
+ '--end',
32
+ type=int,
33
+ required=True,
34
+ help='结束页码 (1-indexed)'
35
+ )
36
+
37
+ args = parser.parse_args()
38
+
39
+ try:
40
+ input_file_path = Path(args.input)
41
+ output_file_path = Path(args.output)
42
+ print(f"--- 正在启动 PDF 提取工具 ---")
43
+ print(f"输入文件:{input_file_path}")
44
+ print(f"输出文件:{output_file_path}")
45
+ print(f"页码范围:{args.start} 到 {args.end}")
46
+
47
+ result = extract_and_save_pages(
48
+ input_file_path=input_file_path,
49
+ output_file_path=output_file_path,
50
+ start_page=args.start,
51
+ end_page=args.end
52
+ )
53
+
54
+ if result is True:
55
+ sys.exit(0)
56
+ else:
57
+ sys.exit(1)
58
+
59
+ except Exception as e:
60
+ print(f"\n发生意外错误:{e}", file=sys.stderr)
61
+ sys.exit(2)
62
+
63
+ if __name__ == '__main__':
64
+ main()