weaver-work-cli 0.1.60 → 0.1.61
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/dist/chunks/{archive-4SQAVTIB.js → archive-S2VKZWQ3.js} +2 -2
- package/dist/chunks/{archive-oa-W2UEJ67L.js → archive-oa-HEDSIUUY.js} +2 -2
- package/dist/chunks/{asset-EGZEABBI.js → asset-HYH4W357.js} +2 -2
- package/dist/chunks/{asset-idle-EBUZBB6M.js → asset-idle-N7REBNGL.js} +2 -2
- package/dist/chunks/{attachment-content-I6WHKGR2.js → attachment-content-VYYA75FR.js} +3 -3
- package/dist/chunks/{blog-2YKMWOWI.js → blog-SLMZZICV.js} +2 -2
- package/dist/chunks/{caizhilian-5NM4X5HG.js → caizhilian-RV43KKBT.js} +4 -4
- package/dist/chunks/{calendar-H4QTJCUY.js → calendar-DMVZ4OLY.js} +2 -2
- package/dist/chunks/{chunk-VLL6JOB4.js → chunk-6XSSAUNW.js} +4 -4
- package/dist/chunks/{chunk-VQWPQTEY.js → chunk-BIA2NKUG.js} +4 -4
- package/dist/chunks/{chunk-VMCCRWFO.js → chunk-BQYX2UX5.js} +2 -2
- package/dist/chunks/{chunk-XF7S6FQW.js → chunk-DTCQFTHW.js} +1 -1
- package/dist/chunks/{chunk-CPG6NLN4.js → chunk-E5YD5JWR.js} +1 -1
- package/dist/chunks/{chunk-PH7N4UFT.js → chunk-EGANE35F.js} +1 -1
- package/dist/chunks/{chunk-Z4IPY5GV.js → chunk-H6YOMI3U.js} +2 -2
- package/dist/chunks/{chunk-HHH4K3YY.js → chunk-ILALC2N6.js} +1 -1
- package/dist/chunks/{chunk-WINR2Q6K.js → chunk-KXJA42EA.js} +86 -19
- package/dist/chunks/{chunk-CQ4OVCKG.js → chunk-KY3R42RS.js} +2 -2
- package/dist/chunks/{chunk-WVH5ESDA.js → chunk-NRYDJN4J.js} +1 -1
- package/dist/chunks/{chunk-TGM7BWEH.js → chunk-P6TXQK75.js} +2 -2
- package/dist/chunks/{chunk-6YWHRXEQ.js → chunk-QBDXJS2W.js} +119 -9
- package/dist/chunks/{chunk-2IGZUMNG.js → chunk-SHX7XBYR.js} +180 -18
- package/dist/chunks/{chunk-P7RXQDCA.js → chunk-T2KVJFWJ.js} +4 -4
- package/dist/chunks/{chunk-FMNCSYIG.js → chunk-TEZHIB6T.js} +2 -2
- package/dist/chunks/{chunk-HMIIVM2L.js → chunk-UBGWIXU3.js} +97 -45
- package/dist/chunks/{chunk-5N222HK6.js → chunk-UBRWONP4.js} +2 -2
- package/dist/chunks/{chunk-7U5YHCW7.js → chunk-UQFPF4EX.js} +2 -2
- package/dist/chunks/{chunk-4GG72JAT.js → chunk-V2LASYKO.js} +2 -2
- package/dist/chunks/{chunk-BDBTZVYA.js → chunk-VBFBMM7G.js} +55 -55
- package/dist/chunks/{chunk-YHSOCGKO.js → chunk-VNYOUAWV.js} +11 -11
- package/dist/chunks/{chunk-SFM3M2L3.js → chunk-WIWDVETN.js} +19 -3
- package/dist/chunks/{chunk-44KJ7WFA.js → chunk-XT77MP54.js} +2 -2
- package/dist/chunks/{chunk-JFKMPRZB.js → chunk-YI2YMW5N.js} +50 -3
- package/dist/chunks/{conflict-QMDLS2EW.js → conflict-GFJRS6I7.js} +2 -2
- package/dist/chunks/{contract-LLRKMLQQ.js → contract-U3ZGNRWB.js} +2 -2
- package/dist/chunks/{cowork-2RDFBZWB.js → cowork-EX7PKIDK.js} +2 -2
- package/dist/chunks/{create-flow-5ODJ7WJO.js → create-flow-EJZD434E.js} +6 -6
- package/dist/chunks/{data-LVDFIRNG.js → data-FOODGAMF.js} +278 -121
- package/dist/chunks/{ebuilder-form-7WA6X6ZI.js → ebuilder-form-I2EOKODG.js} +3 -3
- package/dist/chunks/{ehr-52GBKNJR.js → ehr-TE4RFNID.js} +2 -2
- package/dist/chunks/{entry-mode-73BJML22.js → entry-mode-ZOLSXEGX.js} +9 -5
- package/dist/chunks/{env-OHFS76HV.js → env-TID5NFR6.js} +2 -2
- package/dist/chunks/{esb-HJSXCEOM.js → esb-7L6MHYLB.js} +4 -4
- package/dist/chunks/{factory-FRTA2LP4.js → factory-OG7BPMH5.js} +1 -1
- package/dist/chunks/{fna-OBACRT3J.js → fna-6V2ENR3Y.js} +2 -2
- package/dist/chunks/{group-chat-KBFRPCR2.js → group-chat-5DKPWIHL.js} +3 -3
- package/dist/chunks/{host-TTSGY5NZ.js → host-3DLYA54R.js} +3 -3
- package/dist/chunks/{host-KJT3G5D2.js → host-GOJHHTPY.js} +3 -3
- package/dist/chunks/{host-KOWUMNAY.js → host-OGBHL75G.js} +3 -3
- package/dist/chunks/{host-MFXRA2MY.js → host-ONZC6AE7.js} +3 -3
- package/dist/chunks/{host-W4JCNRFS.js → host-TJ5VVHM7.js} +3 -3
- package/dist/chunks/{host-2BKMPWDH.js → host-VTPAWDZV.js} +3 -3
- package/dist/chunks/{hrm-AK3WGBMN.js → hrm-2N7NFQDZ.js} +3 -3
- package/dist/chunks/{im-JZSBPO4Z.js → im-XCH47ZBN.js} +3 -3
- package/dist/chunks/{invoice-6XDMPCWB.js → invoice-XQRKMGNY.js} +3 -3
- package/dist/chunks/{jiuchuanhui-K4EQU6GK.js → jiuchuanhui-PL6DRPBM.js} +2 -2
- package/dist/chunks/{lazy-dispatch-GCA6HDOB.js → lazy-dispatch-UATEN7H7.js} +5 -5
- package/dist/chunks/{mail-IKVCQCVZ.js → mail-Q7Z4CQ4D.js} +2 -2
- package/dist/chunks/{meeting-ARHVWK7G.js → meeting-5E3BNARL.js} +13 -13
- package/dist/chunks/{minute-doc-547KSYKR.js → minute-doc-NXRE5C77.js} +4 -4
- package/dist/chunks/{okr-KV43FPPX.js → okr-3O3XNYOK.js} +2 -2
- package/dist/chunks/{operations-6NXJ7RTE.js → operations-EZWG72FH.js} +4 -4
- package/dist/chunks/performance-Y7CPNSOR.js +1295 -0
- package/dist/chunks/{plan-WOYR4WEG.js → plan-BLGPZF5M.js} +2 -2
- package/dist/chunks/{project-CQJKBUIL.js → project-6MZMLJMP.js} +2 -2
- package/dist/chunks/{reception-MPBU3JXC.js → reception-F2VT2X52.js} +2 -2
- package/dist/chunks/{recruit-stage-USTTRHOX.js → recruit-stage-4ETIBMIW.js} +2 -2
- package/dist/chunks/{registry-GURQG4ZV.js → registry-3FYSWV3U.js} +3 -3
- package/dist/chunks/{registry-SS5J2FZQ.js → registry-7RS74TCK.js} +4 -4
- package/dist/chunks/{registry-H5HQ5QNH.js → registry-I4DVPDMU.js} +11 -11
- package/dist/chunks/{room-recommend-3UBROMZE.js → room-recommend-GWCVKCXW.js} +4 -4
- package/dist/chunks/{schedule-FTUQML6O.js → schedule-6AN63MJQ.js} +4 -4
- package/dist/chunks/{smart-extract-APX2PNIN.js → smart-extract-SR4SJJKN.js} +3 -3
- package/dist/chunks/{workflow-WMPW3WSB.js → workflow-KF7FA2UQ.js} +7 -7
- package/dist/cmd/refs/index.js +124 -4
- package/dist/core/output.js +199 -26
- package/dist/core/refs.js +36 -0
- package/dist/index.js +38 -33
- package/dist/internal/e10/auth/xiaoe.js +25 -3
- package/dist/shortcuts/caizhilian/operations/files.js +86 -15
- package/dist/shortcuts/data/index.js +12 -2
- package/dist/shortcuts/data/manifest.js +4 -2
- package/dist/shortcuts/data/operations/attribution.js +229 -0
- package/dist/shortcuts/data/operations/ds.js +87 -90
- package/dist/shortcuts/data/operations/index.js +3 -1
- package/dist/shortcuts/index.js +5 -0
- package/dist/shortcuts/meeting/operations/eb/create-flow.js +84 -16
- package/dist/shortcuts/meeting/operations/eb/entry-mode.js +61 -7
- package/dist/shortcuts/performance/continuation.js +74 -0
- package/dist/shortcuts/performance/errors.js +65 -0
- package/dist/shortcuts/performance/host.js +105 -0
- package/dist/shortcuts/performance/index.js +109 -0
- package/dist/shortcuts/performance/manifest.js +30 -0
- package/dist/shortcuts/performance/operations/hrm-resolve.js +114 -0
- package/dist/shortcuts/performance/operations/registry.js +44 -0
- package/dist/shortcuts/performance/operations/scheme-detail.js +70 -0
- package/dist/shortcuts/performance/operations/scheme-draft.js +116 -0
- package/dist/shortcuts/performance/operations/scheme-publish.js +158 -0
- package/dist/shortcuts/performance/operations/types.js +32 -0
- package/dist/shortcuts/performance/scheme-validate.js +307 -0
- package/dist/shortcuts/performance/shared.js +158 -0
- package/docs/agent-io-channel-design.md +67 -3
- package/docs/data.md +2 -1
- package/package.json +1 -1
- package/scripts/xiaoe_env.py +6 -1
- package/skill-template/business-info.json +19 -0
- package/skill-template/domains/performance.md +18 -0
- package/skills/weaver-e10-caizhilian/SKILL.md +1 -1
- package/skills/weaver-e10-data/references/datasource.md +2 -1
- package/skills/weaver-e10-meeting/SKILL.md +5 -3
- package/skills/weaver-e10-performance/SKILL.md +202 -0
- package/skills/weaver-e10-performance/agents/openai.yaml +74 -0
- package/skills/weaver-e10-performance/references/default-assembly.md +108 -0
- package/skills/weaver-e10-performance/references/entity-resolution.md +69 -0
- package/skills/weaver-e10-performance/references/enums-and-normalization.md +80 -0
- package/skills/weaver-e10-performance/references/examples.md +146 -0
- package/skills/weaver-e10-performance/references/extraction-guide.md +144 -0
- package/skills/weaver-e10-performance/references/scheme-edit-req-fields.md +185 -0
- package/skills/weaver-e10-performance/references/scheme-setting-page.md +62 -0
- package/skills/weaver-e10-performance/scripts/parse_appraisal_doc.py +483 -0
- package/skills/weaver-e10-shared/references/json-output-contract.md +5 -0
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""考核文档解析:doc / docx / xls / xlsx / pdf -> Markdown(保留标题层级与表格)。
|
|
4
|
+
|
|
5
|
+
用法:
|
|
6
|
+
python3 parse_appraisal_doc.py <文件路径> [-o 输出.md]
|
|
7
|
+
|
|
8
|
+
注意: 本脚本文件名刻意避开宿主内置 parse_document 工具名,调用方必须通过
|
|
9
|
+
shell 执行本脚本,不要改用宿主内置文档解析工具(无表格编号/标题层级输出)。
|
|
10
|
+
|
|
11
|
+
依赖策略(自动降级):
|
|
12
|
+
docx: python-docx -> 无则用标准库 zipfile+XML 解析
|
|
13
|
+
xlsx: openpyxl -> 无则用标准库 zipfile+XML 解析
|
|
14
|
+
pdf : pdfplumber -> 无则尝试系统 pdftotext 命令(表格保真度下降)
|
|
15
|
+
doc : LibreOffice(soffice) 转 docx 后解析 -> 无则 macOS textutil 转 html 解析(标题层级可能丢失)
|
|
16
|
+
xls : xlrd -> 无则 LibreOffice(soffice) 转 xlsx 后解析
|
|
17
|
+
|
|
18
|
+
输出约定:
|
|
19
|
+
- 标题按原文层级输出 #/##/###
|
|
20
|
+
- 表格输出为 Markdown 管道表并编号(表1、表2…),供后续引用
|
|
21
|
+
- pdf 每页前输出 "--- 第N页 ---" 分隔
|
|
22
|
+
"""
|
|
23
|
+
import argparse
|
|
24
|
+
import os
|
|
25
|
+
import re
|
|
26
|
+
import subprocess
|
|
27
|
+
import sys
|
|
28
|
+
import unicodedata
|
|
29
|
+
import zipfile
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def nfc(s):
|
|
33
|
+
"""文本归一化:PDF/Word 提取常见两类问题——
|
|
34
|
+
1) 组合字符序列 → NFC 合成;
|
|
35
|
+
2) 康熙部首/CJK 部首(⼼U+2FBD 等)混入正文 → 需逐字符 NFKC 映射回统一汉字(⼼→心)。
|
|
36
|
+
只对部首区段做 NFKC,避免全文 NFKC 破坏全角标点等合法字符。"""
|
|
37
|
+
s = unicodedata.normalize("NFC", s)
|
|
38
|
+
out = []
|
|
39
|
+
for ch in s:
|
|
40
|
+
cp = ord(ch)
|
|
41
|
+
if 0x2E80 <= cp <= 0x2FDF:
|
|
42
|
+
out.append(unicodedata.normalize("NFKC", ch))
|
|
43
|
+
else:
|
|
44
|
+
out.append(ch)
|
|
45
|
+
return "".join(out)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def out(s=""):
|
|
49
|
+
sys.stdout.write(s + "\n")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def md_table(rows):
|
|
53
|
+
"""rows: [[c1,c2..], ...] -> markdown 表格文本"""
|
|
54
|
+
if not rows:
|
|
55
|
+
return ""
|
|
56
|
+
lines = []
|
|
57
|
+
lines.append("| " + " | ".join(c.replace("\n", " ").replace("|", "\\|") for c in rows[0]) + " |")
|
|
58
|
+
lines.append("|" + "---|" * len(rows[0]))
|
|
59
|
+
for r in rows[1:]:
|
|
60
|
+
lines.append("| " + " | ".join(c.replace("\n", " ").replace("|", "\\|") for c in r) + " |")
|
|
61
|
+
return "\n".join(lines)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
# ---------------------------------------------------------------- docx
|
|
65
|
+
def parse_docx(path):
|
|
66
|
+
try:
|
|
67
|
+
import docx # python-docx
|
|
68
|
+
return _parse_docx_lib(path)
|
|
69
|
+
except ImportError:
|
|
70
|
+
return _parse_docx_stdlib(path)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _heading_level(style_name):
|
|
74
|
+
if not style_name:
|
|
75
|
+
return 0
|
|
76
|
+
m = re.match(r"^(?:Heading|标题)\s*(\d)", style_name, re.IGNORECASE)
|
|
77
|
+
return int(m.group(1)) if m else 0
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _docx_style_names(z):
|
|
81
|
+
"""解析 styles.xml 得到 styleId -> 样式名 映射。
|
|
82
|
+
中文版 Word 的标题样式 styleId 常是数字(如 val="2" 即'标题 2'),
|
|
83
|
+
必须经 styles.xml 换算成样式名再判断标题级别。"""
|
|
84
|
+
names = {}
|
|
85
|
+
try:
|
|
86
|
+
import xml.etree.ElementTree as ET
|
|
87
|
+
W = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
|
|
88
|
+
root = ET.fromstring(z.read("word/styles.xml"))
|
|
89
|
+
for st in root.findall(W + "style"):
|
|
90
|
+
sid = st.get(W + "styleId")
|
|
91
|
+
nm = st.find(W + "name")
|
|
92
|
+
if sid and nm is not None:
|
|
93
|
+
names[sid] = nm.get(W + "val", "")
|
|
94
|
+
except (KeyError, Exception):
|
|
95
|
+
pass
|
|
96
|
+
return names
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _parse_docx_lib(path):
|
|
100
|
+
import docx
|
|
101
|
+
d = docx.Document(path)
|
|
102
|
+
table_no = 0
|
|
103
|
+
parts = []
|
|
104
|
+
# 按 body 顺序遍历段落与表格
|
|
105
|
+
from docx.table import Table
|
|
106
|
+
from docx.text.paragraph import Paragraph
|
|
107
|
+
body = d.element.body
|
|
108
|
+
for child in body.iterchildren():
|
|
109
|
+
tag = child.tag.split("}")[-1]
|
|
110
|
+
if tag == "p":
|
|
111
|
+
p = Paragraph(child, d)
|
|
112
|
+
text = nfc(p.text.strip())
|
|
113
|
+
if not text:
|
|
114
|
+
continue
|
|
115
|
+
lvl = _heading_level(p.style.name if p.style is not None else "")
|
|
116
|
+
parts.append(("#" * lvl + " " + text) if lvl else text)
|
|
117
|
+
elif tag == "tbl":
|
|
118
|
+
t = Table(child, d)
|
|
119
|
+
table_no += 1
|
|
120
|
+
rows = []
|
|
121
|
+
for row in t.rows:
|
|
122
|
+
rows.append([nfc(c.text.strip()) for c in row.cells])
|
|
123
|
+
parts.append("**表%d**\n\n%s" % (table_no, md_table(rows)))
|
|
124
|
+
return "\n\n".join(parts)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _parse_docx_stdlib(path):
|
|
128
|
+
import xml.etree.ElementTree as ET
|
|
129
|
+
W = "{http://schemas.openxmlformats.org/wordprocessingml/2006/main}"
|
|
130
|
+
with zipfile.ZipFile(path) as z:
|
|
131
|
+
style_names = _docx_style_names(z)
|
|
132
|
+
xml = z.read("word/document.xml")
|
|
133
|
+
root = ET.fromstring(xml)
|
|
134
|
+
body = root.find(W + "body")
|
|
135
|
+
table_no = 0
|
|
136
|
+
parts = []
|
|
137
|
+
for child in body:
|
|
138
|
+
tag = child.tag.split("}")[-1]
|
|
139
|
+
if tag == "p":
|
|
140
|
+
text = nfc("".join(t.text or "" for t in child.iter(W + "t")).strip())
|
|
141
|
+
if not text:
|
|
142
|
+
continue
|
|
143
|
+
style = child.find(W + "pPr/" + W + "pStyle")
|
|
144
|
+
lvl = 0
|
|
145
|
+
if style is not None:
|
|
146
|
+
# styleId 先经 styles.xml 换算样式名,无映射时按 "HeadingN" 直判
|
|
147
|
+
lvl = _heading_level(style_names.get(style.get(W + "val", "")) or style.get(W + "val", ""))
|
|
148
|
+
parts.append(("#" * lvl + " " + text) if lvl else text)
|
|
149
|
+
elif tag == "tbl":
|
|
150
|
+
table_no += 1
|
|
151
|
+
rows = []
|
|
152
|
+
for tr in child.findall(W + "tr"):
|
|
153
|
+
cells = []
|
|
154
|
+
for tc in tr.findall(W + "tc"):
|
|
155
|
+
cells.append(nfc("".join(t.text or "" for t in tc.iter(W + "t")).strip()))
|
|
156
|
+
rows.append(cells)
|
|
157
|
+
parts.append("**表%d**\n\n%s" % (table_no, md_table(rows)))
|
|
158
|
+
return "\n\n".join(parts)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# ---------------------------------------------------------------- xlsx
|
|
162
|
+
def parse_xlsx(path):
|
|
163
|
+
try:
|
|
164
|
+
import openpyxl
|
|
165
|
+
return _parse_xlsx_lib(path)
|
|
166
|
+
except ImportError:
|
|
167
|
+
return _parse_xlsx_stdlib(path)
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _parse_xlsx_lib(path):
|
|
171
|
+
import openpyxl
|
|
172
|
+
wb = openpyxl.load_workbook(path, data_only=True)
|
|
173
|
+
parts = []
|
|
174
|
+
for ws in wb.worksheets:
|
|
175
|
+
parts.append("## Sheet: %s" % ws.title)
|
|
176
|
+
rows = []
|
|
177
|
+
for row in ws.iter_rows(values_only=True):
|
|
178
|
+
cells = ["" if c is None else str(c).strip() for c in row]
|
|
179
|
+
# 去掉行尾空单元格
|
|
180
|
+
while cells and cells[-1] == "":
|
|
181
|
+
cells.pop()
|
|
182
|
+
if cells:
|
|
183
|
+
rows.append(cells)
|
|
184
|
+
if rows:
|
|
185
|
+
parts.append(md_table(rows))
|
|
186
|
+
else:
|
|
187
|
+
parts.append("(空 sheet)")
|
|
188
|
+
return "\n\n".join(parts)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _col_index(ref):
|
|
192
|
+
"""A1 -> 0, B5 -> 1"""
|
|
193
|
+
n = 0
|
|
194
|
+
for ch in ref:
|
|
195
|
+
if ch.isalpha():
|
|
196
|
+
n = n * 26 + (ord(ch.upper()) - 64)
|
|
197
|
+
else:
|
|
198
|
+
break
|
|
199
|
+
return n - 1
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _parse_xlsx_stdlib(path):
|
|
203
|
+
import xml.etree.ElementTree as ET
|
|
204
|
+
NS = "{http://schemas.openxmlformats.org/spreadsheetml/2006/main}"
|
|
205
|
+
RNS = "{http://schemas.openxmlformats.org/officeDocument/2006/relationships}"
|
|
206
|
+
with zipfile.ZipFile(path) as z:
|
|
207
|
+
shared = []
|
|
208
|
+
if "xl/sharedStrings.xml" in z.namelist():
|
|
209
|
+
sroot = ET.fromstring(z.read("xl/sharedStrings.xml"))
|
|
210
|
+
for si in sroot.findall(NS + "si"):
|
|
211
|
+
shared.append("".join(t.text or "" for t in si.iter(NS + "t")))
|
|
212
|
+
wb = ET.fromstring(z.read("xl/workbook.xml"))
|
|
213
|
+
rels = ET.fromstring(z.read("xl/_rels/workbook.xml.rels"))
|
|
214
|
+
rel_map = {r.get("Id"): r.get("Target") for r in rels}
|
|
215
|
+
parts = []
|
|
216
|
+
for sheet in wb.find(NS + "sheets"):
|
|
217
|
+
name = sheet.get("name")
|
|
218
|
+
rid = sheet.get(RNS + "id")
|
|
219
|
+
target = rel_map.get(rid, "")
|
|
220
|
+
if target.startswith("/"):
|
|
221
|
+
target = target[1:]
|
|
222
|
+
elif not target.startswith("xl/"):
|
|
223
|
+
target = "xl/" + target
|
|
224
|
+
if target not in z.namelist():
|
|
225
|
+
continue
|
|
226
|
+
parts.append("## Sheet: %s" % name)
|
|
227
|
+
sroot = ET.fromstring(z.read(target))
|
|
228
|
+
rows = []
|
|
229
|
+
for r in sroot.iter(NS + "row"):
|
|
230
|
+
cells_by_idx = {}
|
|
231
|
+
for c in r.findall(NS + "c"):
|
|
232
|
+
idx = _col_index(c.get("r", "A1"))
|
|
233
|
+
v = c.find(NS + "v")
|
|
234
|
+
txt = ""
|
|
235
|
+
if v is not None and v.text is not None:
|
|
236
|
+
if c.get("t") == "s":
|
|
237
|
+
txt = shared[int(v.text)]
|
|
238
|
+
else:
|
|
239
|
+
txt = v.text
|
|
240
|
+
elif c.get("t") == "inlineStr":
|
|
241
|
+
is_el = c.find(NS + "is")
|
|
242
|
+
if is_el is not None:
|
|
243
|
+
txt = "".join(t.text or "" for t in is_el.iter(NS + "t"))
|
|
244
|
+
cells_by_idx[idx] = (txt or "").strip()
|
|
245
|
+
if cells_by_idx:
|
|
246
|
+
width = max(cells_by_idx) + 1
|
|
247
|
+
rows.append([cells_by_idx.get(i, "") for i in range(width)])
|
|
248
|
+
if rows:
|
|
249
|
+
parts.append(md_table(rows))
|
|
250
|
+
else:
|
|
251
|
+
parts.append("(空 sheet)")
|
|
252
|
+
return "\n\n".join(parts)
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
# ---------------------------------------------------------------- pdf
|
|
256
|
+
def parse_pdf(path):
|
|
257
|
+
try:
|
|
258
|
+
import pdfplumber
|
|
259
|
+
except ImportError:
|
|
260
|
+
return _parse_pdf_pdftotext(path)
|
|
261
|
+
parts = []
|
|
262
|
+
with pdfplumber.open(path) as pdf:
|
|
263
|
+
for i, page in enumerate(pdf.pages, 1):
|
|
264
|
+
parts.append("--- 第%d页 ---" % i)
|
|
265
|
+
tables = [[nfc(str(c)) if c is not None else "" for c in r] for t in (page.extract_tables() or []) if t for r in t]
|
|
266
|
+
page_text = nfc(page.extract_text() or "")
|
|
267
|
+
lines = []
|
|
268
|
+
for ln in page_text.splitlines():
|
|
269
|
+
if not ln.strip() or ln.strip() not in tables:
|
|
270
|
+
lines.append(ln)
|
|
271
|
+
parts.append("\n".join(lines))
|
|
272
|
+
table_no = 0
|
|
273
|
+
for t in (page.extract_tables() or []):
|
|
274
|
+
if not t:
|
|
275
|
+
continue
|
|
276
|
+
table_no += 1
|
|
277
|
+
parts.append("**表%d(第%d页)**\n\n%s" % (
|
|
278
|
+
table_no, i, md_table([[nfc(str(c)) if c is not None else "" for c in r] for r in t])))
|
|
279
|
+
return "\n\n".join(parts)
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _parse_pdf_pdftotext(path):
|
|
283
|
+
try:
|
|
284
|
+
r = subprocess.run(["pdftotext", "-layout", path, "-"], capture_output=True, timeout=120)
|
|
285
|
+
if r.returncode != 0:
|
|
286
|
+
raise RuntimeError(r.stderr.decode("utf-8", "ignore"))
|
|
287
|
+
text = r.stdout.decode("utf-8", "ignore")
|
|
288
|
+
if not text.strip():
|
|
289
|
+
raise RuntimeError("empty text(可能是扫描件)")
|
|
290
|
+
return text + "\n\n(注:本 pdf 由 pdftotext 解析,表格结构可能失真,请人工核对指标表格)"
|
|
291
|
+
except FileNotFoundError:
|
|
292
|
+
raise RuntimeError("解析 pdf 需要安装 pdfplumber(pip3 install pdfplumber)或系统 pdftotext(poppler)")
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
# ---------------------------------------------------------------- doc / xls(旧版二进制格式)
|
|
296
|
+
import shutil
|
|
297
|
+
import tempfile
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def _convert_via_soffice(path, target_ext):
|
|
301
|
+
"""LibreOffice 无头转换 doc->docx / xls->xlsx;失败返回 None。"""
|
|
302
|
+
soffice = shutil.which("soffice") or shutil.which("libreoffice")
|
|
303
|
+
if not soffice:
|
|
304
|
+
return None
|
|
305
|
+
with tempfile.TemporaryDirectory() as td:
|
|
306
|
+
try:
|
|
307
|
+
r = subprocess.run(
|
|
308
|
+
[soffice, "--headless", "--convert-to", target_ext, "--outdir", td, path],
|
|
309
|
+
capture_output=True, timeout=180)
|
|
310
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
311
|
+
return None
|
|
312
|
+
if r.returncode != 0:
|
|
313
|
+
return None
|
|
314
|
+
base = os.path.splitext(os.path.basename(path))[0] + "." + target_ext
|
|
315
|
+
converted = os.path.join(td, base)
|
|
316
|
+
if not os.path.isfile(converted):
|
|
317
|
+
return None
|
|
318
|
+
# 读出内容后立刻解析(临时目录随之销毁,不在用户目录留文件)
|
|
319
|
+
return parse_docx(converted) if target_ext == "docx" else parse_xlsx(converted)
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def parse_doc(path):
|
|
323
|
+
# ① LibreOffice 转 docx(表格/标题保真最好)
|
|
324
|
+
content = _convert_via_soffice(path, "docx")
|
|
325
|
+
if content:
|
|
326
|
+
return content
|
|
327
|
+
# ② macOS 自带 textutil 转 html(表格保留,标题层级可能丢失)
|
|
328
|
+
if shutil.which("textutil"):
|
|
329
|
+
try:
|
|
330
|
+
r = subprocess.run(["textutil", "-convert", "html", "-stdout", path],
|
|
331
|
+
capture_output=True, timeout=120)
|
|
332
|
+
if r.returncode == 0 and r.stdout:
|
|
333
|
+
return _parse_html(r.stdout.decode("utf-8", "ignore"))
|
|
334
|
+
except (OSError, subprocess.TimeoutExpired):
|
|
335
|
+
pass
|
|
336
|
+
raise RuntimeError(
|
|
337
|
+
"解析 .doc 需要 LibreOffice(soffice)或 macOS textutil;"
|
|
338
|
+
"都没有时可先在 Word 里另存为 .docx 再试")
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _parse_html(html):
|
|
342
|
+
"""stdlib HTMLParser:抽取 <h1-h6> 标题与 <table> 行为 Markdown。"""
|
|
343
|
+
from html.parser import HTMLParser
|
|
344
|
+
|
|
345
|
+
class P(HTMLParser):
|
|
346
|
+
def __init__(self):
|
|
347
|
+
super().__init__()
|
|
348
|
+
self.parts = []
|
|
349
|
+
self.table_no = 0
|
|
350
|
+
self.rows = None # 当前表格行集合
|
|
351
|
+
self.row = None # 当前行
|
|
352
|
+
self.cell = None # 当前单元格文本
|
|
353
|
+
self.heading = None # 当前标题 (level, buf)
|
|
354
|
+
self.para = None # 当前段落文本
|
|
355
|
+
|
|
356
|
+
def handle_starttag(self, tag, attrs):
|
|
357
|
+
if tag == "table":
|
|
358
|
+
self._flush_para()
|
|
359
|
+
self.rows = []
|
|
360
|
+
elif tag == "tr" and self.rows is not None:
|
|
361
|
+
self.row = []
|
|
362
|
+
elif tag in ("td", "th") and self.row is not None:
|
|
363
|
+
self.cell = []
|
|
364
|
+
elif tag in ("h1", "h2", "h3", "h4", "h5", "h6"):
|
|
365
|
+
self._flush_para()
|
|
366
|
+
self.heading = (int(tag[1]), [])
|
|
367
|
+
elif tag == "p":
|
|
368
|
+
self._flush_para()
|
|
369
|
+
self.para = []
|
|
370
|
+
|
|
371
|
+
def handle_endtag(self, tag):
|
|
372
|
+
if tag == "table" and self.rows is not None:
|
|
373
|
+
if self.rows:
|
|
374
|
+
self.table_no += 1
|
|
375
|
+
self.parts.append("**表%d**\n\n%s" % (self.table_no, md_table(self.rows)))
|
|
376
|
+
self.rows = None
|
|
377
|
+
elif tag == "tr" and self.row is not None:
|
|
378
|
+
if any(c.strip() for c in self.row):
|
|
379
|
+
self.rows.append([nfc(c.strip()) for c in self.row])
|
|
380
|
+
self.row = None
|
|
381
|
+
elif tag in ("td", "th") and self.cell is not None:
|
|
382
|
+
self.row.append("".join(self.cell))
|
|
383
|
+
self.cell = None
|
|
384
|
+
elif tag in ("h1", "h2", "h3", "h4", "h5", "h6") and self.heading:
|
|
385
|
+
lvl, buf = self.heading
|
|
386
|
+
text = nfc("".join(buf).strip())
|
|
387
|
+
if text:
|
|
388
|
+
self.parts.append("#" * lvl + " " + text)
|
|
389
|
+
self.heading = None
|
|
390
|
+
elif tag == "p":
|
|
391
|
+
self._flush_para()
|
|
392
|
+
|
|
393
|
+
def handle_data(self, data):
|
|
394
|
+
if self.cell is not None:
|
|
395
|
+
self.cell.append(data)
|
|
396
|
+
elif self.heading:
|
|
397
|
+
self.heading[1].append(data)
|
|
398
|
+
elif self.para is not None:
|
|
399
|
+
self.para.append(data)
|
|
400
|
+
|
|
401
|
+
def _flush_para(self):
|
|
402
|
+
if self.para is not None:
|
|
403
|
+
text = nfc("".join(self.para).strip())
|
|
404
|
+
if text:
|
|
405
|
+
self.parts.append(text)
|
|
406
|
+
self.para = None
|
|
407
|
+
|
|
408
|
+
p = P()
|
|
409
|
+
p.feed(html)
|
|
410
|
+
p.close()
|
|
411
|
+
p._flush_para()
|
|
412
|
+
return "\n\n".join(p.parts)
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+
def parse_xls(path):
|
|
416
|
+
# ① xlrd(专支持旧版 .xls,若已安装)
|
|
417
|
+
try:
|
|
418
|
+
import xlrd # type: ignore
|
|
419
|
+
parts = []
|
|
420
|
+
book = xlrd.open_workbook(path)
|
|
421
|
+
for ws in book.sheets():
|
|
422
|
+
parts.append("## Sheet: %s" % ws.name)
|
|
423
|
+
rows = []
|
|
424
|
+
for ri in range(ws.nrows):
|
|
425
|
+
cells = [nfc(str(c.value).strip()) for c in ws.row(ri)]
|
|
426
|
+
while cells and cells[-1] == "":
|
|
427
|
+
cells.pop()
|
|
428
|
+
if cells:
|
|
429
|
+
rows.append(cells)
|
|
430
|
+
parts.append(md_table(rows) if rows else "(空 sheet)")
|
|
431
|
+
return "\n\n".join(parts)
|
|
432
|
+
except ImportError:
|
|
433
|
+
pass
|
|
434
|
+
# ② LibreOffice 转 xlsx 后按 xlsx 解析
|
|
435
|
+
content = _convert_via_soffice(path, "xlsx")
|
|
436
|
+
if content:
|
|
437
|
+
return content
|
|
438
|
+
raise RuntimeError(
|
|
439
|
+
"解析 .xls 需要安装 xlrd(pip3 install xlrd)或 LibreOffice(soffice);"
|
|
440
|
+
"都没有时可先在 Excel 里另存为 .xlsx 再试")
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
# ---------------------------------------------------------------- main
|
|
444
|
+
def main():
|
|
445
|
+
ap = argparse.ArgumentParser(description="考核文档解析为 Markdown")
|
|
446
|
+
ap.add_argument("file", help="doc / docx / xls / xlsx / pdf 文件路径")
|
|
447
|
+
ap.add_argument("-o", "--output", help="输出到文件(默认打印到标准输出)")
|
|
448
|
+
args = ap.parse_args()
|
|
449
|
+
|
|
450
|
+
if not os.path.isfile(args.file):
|
|
451
|
+
out("ERROR: 文件不存在: %s" % args.file)
|
|
452
|
+
sys.exit(2)
|
|
453
|
+
ext = os.path.splitext(args.file)[1].lower()
|
|
454
|
+
try:
|
|
455
|
+
if ext == ".docx":
|
|
456
|
+
content = parse_docx(args.file)
|
|
457
|
+
elif ext == ".doc":
|
|
458
|
+
content = parse_doc(args.file)
|
|
459
|
+
elif ext == ".xlsx":
|
|
460
|
+
content = parse_xlsx(args.file)
|
|
461
|
+
elif ext == ".xls":
|
|
462
|
+
content = parse_xls(args.file)
|
|
463
|
+
elif ext == ".pdf":
|
|
464
|
+
content = parse_pdf(args.file)
|
|
465
|
+
else:
|
|
466
|
+
raise RuntimeError("不支持的格式 %s(支持 doc/docx/xls/xlsx/pdf)" % ext)
|
|
467
|
+
except RuntimeError as e:
|
|
468
|
+
out("ERROR: %s" % e)
|
|
469
|
+
sys.exit(1)
|
|
470
|
+
except Exception as e: # noqa
|
|
471
|
+
out("ERROR: 解析失败: %s: %s" % (type(e).__name__, e))
|
|
472
|
+
sys.exit(1)
|
|
473
|
+
|
|
474
|
+
if args.output:
|
|
475
|
+
with open(args.output, "w", encoding="utf-8") as f:
|
|
476
|
+
f.write(content)
|
|
477
|
+
out("OK: 已写入 %s(%d 字符)" % (args.output, len(content)))
|
|
478
|
+
else:
|
|
479
|
+
out(content)
|
|
480
|
+
|
|
481
|
+
|
|
482
|
+
if __name__ == "__main__":
|
|
483
|
+
main()
|
|
@@ -119,6 +119,11 @@ Windows/PowerShell 下不要使用 `printf`、bash 反斜杠续行、`$HOME/...`
|
|
|
119
119
|
- 列表结果只展示最相关的前 `10` 条;如果用户要求更多,分批展示并说明还可以继续读取。
|
|
120
120
|
- 详情结果只展示与用户问题相关的字段。发票类详情通常优先展示 `fid`、号码/代码、购销方、金额、日期、查验/报销状态和附件摘要。
|
|
121
121
|
- 大结果会由 CLI 暂存在用户级 state 目录,并返回 `refId`。后续操作用 `--input-ref <refId>` 和可选的 `--pick` / `--pick-as` 取所需数据;不要直接读取暂存文件,也不要用 `head` / `tail` 截取 JSON。
|
|
122
|
+
- **预览条数 ≠ 返回条数(最容易误判的一条)**:结果超过 stdout 预算(默认 24 KB)时,CLI 会先压字段(长文本截短、**条目不丢**),仍然装不下才裁到 50/20/10/5/2/1 条。发生裁条目时,信封顶部一定有 `previewTruncated: true`、`previewOmitted` 和带原始条数的 `largeOutputNotice`(形如 `data.items 共 92 条,仅显示 5 条`)。**看到 `previewOmitted` 就说明 `data.items.length` 只是预览长度**:不得据此断言「服务端只返回 N 条」「分页参数没生效」「只成功 N 人」,必须先用 `refs get <refId> data.items` 取全量再下结论。
|
|
123
|
+
- **`refs get` 取数组会自动分批,且每批是完整条目**:返回 `{ total, offset, returned, hasMore, nextOffset, data }`,按 `largeOutputNotice` 给的命令续读(如 `refs get <refId> data.items --offset 33 --limit 33`)直到 `hasMore:false`。**不要**用先取到的这一批的 `length` 当总数,`total` 才是。
|
|
124
|
+
- **预览中的长文本不是完整值**:`previewTruncatedFields` 按字段路径给出 `{ unit:"chars", total, shown }`。需要原文时用 `refs get <refId> <path> --char-offset 0 --char-limit <n>` 分段读取,按 `nextCharOffset` 续读到 `hasMore:false`;不得把预览前缀当成原始字段做比较、总结或写入。
|
|
125
|
+
- **有 `refId` 就不得重查业务接口**:全量、子字段和长文本都从本地 ref 取;只有 ref 过期/损坏且任务确实仍需要数据时,才能重新请求服务端。
|
|
126
|
+
- **暂存不会无界增长**:只有超过 stdout 预算的结果才默认落盘;默认 TTL 30 分钟、最多 50 个 ref/总计 200 MB。CLI 在后续命令中惰性清理,也可用 `refs prune --all` 手动清理。
|
|
122
127
|
- 如果用户确实需要一个可交付文件,使用业务 operation 提供的下载/导出参数或用户指定的输出路径;不要把 CLI stdout 当成临时中转文件。
|
|
123
128
|
- 如果为了调试必须引用原始 envelope,只截取必要字段:`ok`、`operation`、`meta`、`warnings`、`error` 或 `data` 的相关子树。
|
|
124
129
|
|