Skip to content

Commit 8bfe862

Browse files
committed
feat(cli): add po command line interface
Add a Python-written CLI to python-office with zero invasion of existing code (see #1 for the design, #2 for the supply-chain audit done first): - office/cli/: declarative registry (commands.py), argparse builder + lazy dispatch (main.py), text/--json output layer (output.py) - office/__main__.py: `python -m office` alias - setup.cfg: console_scripts entry `po = office.cli.main:main` - first batch covers pdf (11 cmds), excel (7 cmds), tools (4 cmds); Windows-only modules are platform-guarded with clean errors - import-time banners and library prints are redirected to stderr in --json mode so stdout stays parseable - tests/test_code/test_cli.py: 33 tests (registry integrity, parser, dispatch via stubbed po* deps, json/error paths, platform guard); runs without any po* packages installed
1 parent 0a42cc7 commit 8bfe862

9 files changed

Lines changed: 825 additions & 0 deletions

File tree

‎README-CN.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,22 @@ pip install poexcel # 只用 Excel 处理
158158
159159
---
160160

161+
## 💻 命令行:`po` 命令
162+
163+
安装 python-office 后自带终端命令,不写代码也能用:
164+
165+
```bash
166+
po pdf merge2pdf a.pdf b.pdf --output-file merged.pdf
167+
po excel excel2pdf 报表.xlsx 报表.pdf
168+
po tools url2ip www.python-office.com
169+
170+
po --json tools url2ip www.python-office.com # 单行 JSON 输出,方便脚本和 agent 调用
171+
```
172+
173+
`po --help` 查看全部模块与子命令(`python -m office` 为等价入口)。
174+
175+
---
176+
161177
## 🚀 Quick Start(5 行代码体验)
162178

163179
```python

‎README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ pip install poexcel # Excel processing only
154154
155155
---
156156

157+
## 💻 CLI: the `po` command
158+
159+
Installing python-office also gives you a terminal command — no Python code needed:
160+
161+
```bash
162+
po pdf merge2pdf a.pdf b.pdf --output-file merged.pdf
163+
po excel excel2pdf report.xlsx report.pdf
164+
po tools url2ip www.python-office.com
165+
166+
po --json tools url2ip www.python-office.com # one-line JSON for scripts & agents
167+
```
168+
169+
`po --help` lists all modules and commands (`python -m office` works as an alias).
170+
171+
---
172+
157173
## 🚀 Quick Start
158174

159175
```python

‎office/__main__.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: UTF-8 -*-
2+
"""支持 ``python -m office`` 作为 ``po`` 命令的等价入口。"""
3+
4+
import sys
5+
6+
from office.cli.main import main
7+
8+
if __name__ == "__main__":
9+
sys.exit(main())

‎office/cli/__init__.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# -*- coding: UTF-8 -*-
2+
"""python-office 的命令行接口(`po` 命令)。
3+
4+
Command line interface of python-office (the `po` command).
5+
6+
该子包只依赖 ``office.api.*`` 的公开函数,不修改任何现有模块。
7+
"""

‎office/cli/commands.py‎

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# -*- coding: UTF-8 -*-
2+
"""`po` 命令的声明式注册表。
3+
4+
Declarative registry of the `po` command.
5+
6+
注册表自包含全部参数信息:构建 argparse 解析器时无需 import 任何 ``office.api``
7+
模块(只有真正分发调用时才延迟导入),因此 ``po --help`` 的构建过程零依赖。
8+
9+
每个模块条目的字段:
10+
11+
- ``module``:延迟导入的 ``office.api`` 模块路径
12+
- ``help``:模块级帮助文本
13+
- ``platforms``:``None`` 表示全平台;否则为允许的 ``sys.platform`` 列表
14+
- ``commands``:子命令名 -> {help, params}
15+
16+
参数字段:
17+
18+
- ``name``:对应 api 函数的形参名(分发时按此名传 kwargs)
19+
- ``positional``:True 为位置参数,否则为 ``--kebab-case`` 选项
20+
- ``flag``:store_true 布尔开关(仅选项)
21+
- ``type`` / ``nargs`` / ``default`` / ``choices`` / ``required``:直传 argparse
22+
23+
刻意不收录的函数:交互式或长任务(weather、net_speed_test 等)、已废弃
24+
(open_soft 等)、参数为遗留兼容别名的(add_mark / add_watermark)。
25+
"""
26+
27+
MODULES = {
28+
"pdf": {
29+
"module": "office.api.pdf",
30+
"help": "PDF 处理:合并 / 拆分 / 格式转换 / 水印 / 加密",
31+
"platforms": None,
32+
"commands": {
33+
"pdf2docx": {
34+
"help": "PDF 转 Word / Convert PDF to docx",
35+
"params": [
36+
{"name": "input_file", "positional": True, "metavar": "PDF",
37+
"help": "输入 PDF 文件 / input PDF file"},
38+
{"name": "output_file", "help": "输出 docx 文件 / output docx file"},
39+
],
40+
},
41+
"pdf2imgs": {
42+
"help": "PDF 转图片 / Convert PDF to images",
43+
"params": [
44+
{"name": "input_file", "positional": True, "metavar": "PDF",
45+
"help": "输入 PDF 文件 / input PDF file"},
46+
{"name": "output_file", "help": "输出图片(或目录)/ output image or dir"},
47+
{"name": "merge", "flag": True,
48+
"help": "合并为长图 / merge into one long image"},
49+
],
50+
},
51+
"txt2pdf": {
52+
"help": "文本文件转 PDF / Convert txt to PDF",
53+
"params": [
54+
{"name": "input_file", "positional": True, "metavar": "TXT",
55+
"help": "输入 txt 文件 / input txt file"},
56+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
57+
],
58+
},
59+
"split4pdf": {
60+
"help": "按页码拆分 PDF / Split PDF by page range",
61+
"params": [
62+
{"name": "input_file", "positional": True, "metavar": "PDF",
63+
"help": "输入 PDF 文件 / input PDF file"},
64+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
65+
{"name": "from_page", "type": int, "default": -1,
66+
"help": "起始页(0 起)/ first page (0-based)"},
67+
{"name": "to_page", "type": int, "default": -1,
68+
"help": "结束页 / last page"},
69+
],
70+
},
71+
"merge2pdf": {
72+
"help": "合并多个 PDF / Merge multiple PDF files",
73+
"params": [
74+
{"name": "input_file_list", "positional": True, "nargs": "+",
75+
"metavar": "PDF",
76+
"help": "输入 PDF 文件列表 / input PDF files"},
77+
{"name": "output_file", "default": "merged.pdf",
78+
"help": "输出 PDF 文件,默认 merged.pdf / output file"},
79+
],
80+
},
81+
"del4pdf": {
82+
"help": "删除指定页面 / Delete pages from PDF",
83+
"params": [
84+
{"name": "input_file", "positional": True, "metavar": "PDF",
85+
"help": "输入 PDF 文件 / input PDF file"},
86+
{"name": "page_nums", "required": True, "nargs": "+", "type": int,
87+
"metavar": "PAGE",
88+
"help": "要删除的页码(0 起)/ page numbers to delete"},
89+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
90+
],
91+
},
92+
"encrypt4pdf": {
93+
"help": "加密 PDF / Encrypt PDF with password",
94+
"params": [
95+
{"name": "input_file", "positional": True, "metavar": "PDF",
96+
"help": "输入 PDF 文件 / input PDF file"},
97+
{"name": "password", "required": True,
98+
"help": "密码 / password"},
99+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
100+
],
101+
},
102+
"decrypt4pdf": {
103+
"help": "解密 PDF / Decrypt PDF",
104+
"params": [
105+
{"name": "input_file", "positional": True, "metavar": "PDF",
106+
"help": "输入 PDF 文件 / input PDF file"},
107+
{"name": "password", "required": True,
108+
"help": "密码 / password"},
109+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
110+
],
111+
},
112+
"add_text_watermark": {
113+
"help": "添加文字水印 / Add text watermark",
114+
"params": [
115+
{"name": "input_file", "positional": True, "metavar": "PDF",
116+
"help": "输入 PDF 文件 / input PDF file"},
117+
{"name": "text", "default": "python-office",
118+
"help": "水印文字 / watermark text"},
119+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
120+
{"name": "fontname", "default": "Helvetica",
121+
"help": "字体名 / font name"},
122+
{"name": "fontsize", "type": int, "default": 12,
123+
"help": "字号 / font size"},
124+
],
125+
},
126+
"add_img_water": {
127+
"help": "添加图片水印 / Add image watermark",
128+
"params": [
129+
{"name": "input_file", "positional": True, "metavar": "PDF",
130+
"help": "输入 PDF 文件 / input PDF file"},
131+
{"name": "mark_file", "required": True,
132+
"help": "水印图片 / watermark image"},
133+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
134+
],
135+
},
136+
"add_watermark_by_parameters": {
137+
"help": "参数化文字水印 / Add watermark by parameters",
138+
"params": [
139+
{"name": "input_file", "positional": True, "metavar": "PDF",
140+
"help": "输入 PDF 文件 / input PDF file"},
141+
{"name": "mark_str", "required": True,
142+
"help": "水印文字 / watermark text"},
143+
{"name": "output_file", "help": "输出 PDF 文件 / output PDF file"},
144+
],
145+
},
146+
},
147+
},
148+
"excel": {
149+
"module": "office.api.excel",
150+
"help": "Excel 处理:模拟数据 / 合并拆分 / 格式转换 / 内容检索",
151+
"platforms": None,
152+
"commands": {
153+
"fake2excel": {
154+
"help": "生成模拟数据 Excel / Create Excel with fake data",
155+
"params": [
156+
{"name": "columns", "nargs": "+", "default": ["name"],
157+
"metavar": "COL",
158+
"help": "列名列表,见文档 / column names"},
159+
{"name": "rows", "type": int, "default": 1,
160+
"help": "行数 / number of rows"},
161+
{"name": "path", "default": "./fake2excel.xlsx",
162+
"help": "输出文件 / output file"},
163+
{"name": "language", "default": "zh_CN",
164+
"choices": ["zh_CN", "english"],
165+
"help": "数据语言 / data language"},
166+
],
167+
},
168+
"merge2excel": {
169+
"help": "多个 Excel 合并为多 sheet / Merge Excel files (one sheet each)",
170+
"params": [
171+
{"name": "dir_path", "positional": True, "metavar": "DIR",
172+
"help": "输入目录 / input directory"},
173+
{"name": "output_file", "default": "merge2excel.xlsx",
174+
"help": "输出文件 / output file"},
175+
],
176+
},
177+
"sheet2excel": {
178+
"help": "按 sheet 拆分为多文件 / Split sheets into files",
179+
"params": [
180+
{"name": "file_path", "positional": True, "metavar": "XLSX",
181+
"help": "输入 Excel 文件 / input Excel file"},
182+
{"name": "output_path", "default": "./",
183+
"help": "输出目录 / output directory"},
184+
],
185+
},
186+
"merge2sheet": {
187+
"help": "多个 Excel 合并到一个 sheet / Merge files into one sheet",
188+
"params": [
189+
{"name": "dir_path", "positional": True, "metavar": "DIR",
190+
"help": "输入目录 / input directory"},
191+
{"name": "output_sheet_name", "default": "Sheet1",
192+
"help": "输出 sheet 名 / output sheet name"},
193+
{"name": "output_excel_name", "default": "merge2sheet",
194+
"help": "输出文件名 / output file name"},
195+
],
196+
},
197+
"find_excel_data": {
198+
"help": "搜索 Excel 内容 / Search content in Excel files",
199+
"params": [
200+
{"name": "search_key", "positional": True, "metavar": "KEY",
201+
"help": "搜索关键词 / search key"},
202+
{"name": "target_dir", "positional": True, "metavar": "DIR",
203+
"help": "搜索目录 / directory to search"},
204+
],
205+
},
206+
"split_excel_by_column": {
207+
"help": "按列值拆分 Excel / Split Excel by column values",
208+
"params": [
209+
{"name": "filepath", "positional": True, "metavar": "XLSX",
210+
"help": "输入 Excel 文件 / input Excel file"},
211+
{"name": "column", "positional": True, "type": int, "metavar": "COL",
212+
"help": "列号(0 起)/ column index (0-based)"},
213+
{"name": "worksheet_name",
214+
"help": "sheet 名,默认第一个 / worksheet name"},
215+
],
216+
},
217+
"excel2pdf": {
218+
"help": "Excel 转 PDF / Convert Excel to PDF",
219+
"params": [
220+
{"name": "excel_path", "positional": True, "metavar": "XLSX",
221+
"help": "输入 Excel 文件 / input Excel file"},
222+
{"name": "pdf_path", "positional": True, "metavar": "PDF",
223+
"help": "输出 PDF 文件 / output PDF file"},
224+
{"name": "sheet_id", "type": int, "default": 0,
225+
"help": "sheet 序号(0 起)/ sheet index (0-based)"},
226+
],
227+
},
228+
},
229+
},
230+
"tools": {
231+
"module": "office.api.tools",
232+
"help": "实用工具:翻译 / IP 解析 / 二维码 / 密码生成",
233+
"platforms": None,
234+
"commands": {
235+
"url2ip": {
236+
"help": "域名解析 IP / Resolve URL to IP address",
237+
"params": [
238+
{"name": "url", "positional": True, "metavar": "URL",
239+
"help": "URL 或域名 / URL or domain"},
240+
],
241+
},
242+
"transtools": {
243+
"help": "文本翻译 / Translate text",
244+
"params": [
245+
{"name": "to_lang", "positional": True, "metavar": "LANG",
246+
"help": "目标语言 / target language"},
247+
{"name": "content", "positional": True, "metavar": "TEXT",
248+
"help": "待翻译内容 / content to translate"},
249+
{"name": "from_lang", "default": "zh",
250+
"help": "源语言 / source language"},
251+
],
252+
},
253+
"qrcodetools": {
254+
"help": "URL 转二维码 / Generate QR code for URL",
255+
"params": [
256+
{"name": "url", "positional": True, "metavar": "URL",
257+
"help": "URL / URL to encode"},
258+
{"name": "output", "default": "./qrcode_img.png",
259+
"help": "输出图片 / output image"},
260+
],
261+
},
262+
"passwordtools": {
263+
"help": "生成随机密码 / Generate random password",
264+
"params": [
265+
{"name": "len", "type": int, "default": 8,
266+
"help": "密码长度 / password length"},
267+
],
268+
},
269+
},
270+
},
271+
}

0 commit comments

Comments
 (0)