ofdkit 0.1.0__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.
- ofdkit-0.1.0/LICENSE +202 -0
- ofdkit-0.1.0/PKG-INFO +186 -0
- ofdkit-0.1.0/README.md +160 -0
- ofdkit-0.1.0/pyproject.toml +40 -0
- ofdkit-0.1.0/src/ofdkit/__init__.py +54 -0
- ofdkit-0.1.0/src/ofdkit/__main__.py +5 -0
- ofdkit-0.1.0/src/ofdkit/_xml.py +120 -0
- ofdkit-0.1.0/src/ofdkit/batch.py +111 -0
- ofdkit-0.1.0/src/ofdkit/cli.py +189 -0
- ofdkit-0.1.0/src/ofdkit/document.py +509 -0
- ofdkit-0.1.0/src/ofdkit/errors.py +13 -0
- ofdkit-0.1.0/src/ofdkit/invoice.py +438 -0
- ofdkit-0.1.0/src/ofdkit/layout.py +84 -0
- ofdkit-0.1.0/src/ofdkit/markdown.py +100 -0
- ofdkit-0.1.0/src/ofdkit/markitdown_plugin.py +55 -0
- ofdkit-0.1.0/src/ofdkit/mcp_server.py +125 -0
- ofdkit-0.1.0/src/ofdkit/package.py +145 -0
ofdkit-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
ofdkit-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ofdkit
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Read OFD documents (GB/T 33190) and Chinese e-invoices: text, invoice fields, and AI tool integrations.
|
|
5
|
+
Keywords: ofd,gb/t 33190,e-invoice,fapiao,电子发票,数电票,mcp
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Natural Language :: Chinese (Simplified)
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Topic :: Office/Business :: Financial :: Accounting
|
|
15
|
+
Classifier: Topic :: Text Processing
|
|
16
|
+
Requires-Dist: markitdown>=0.1,<0.2 ; extra == 'markitdown'
|
|
17
|
+
Requires-Dist: mcp>=2.3,<3 ; extra == 'mcp'
|
|
18
|
+
Maintainer: Edgar-311
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Project-URL: Homepage, https://github.com/Edgar-311/ofdkit
|
|
21
|
+
Project-URL: Issues, https://github.com/Edgar-311/ofdkit/issues
|
|
22
|
+
Project-URL: Changelog, https://github.com/Edgar-311/ofdkit/blob/main/CHANGELOG.md
|
|
23
|
+
Provides-Extra: markitdown
|
|
24
|
+
Provides-Extra: mcp
|
|
25
|
+
Description-Content-Type: text/markdown
|
|
26
|
+
|
|
27
|
+
# ofdkit
|
|
28
|
+
|
|
29
|
+
轻量的 Python 工具,用来读取 OFD 文件,提取里面的文字和发票字段,并能接入 AI 工具。OFD 是中国的国家标准版式文档格式(GB/T 33190),常见于电子发票和公文。
|
|
30
|
+
|
|
31
|
+
A lightweight Python toolkit for reading OFD documents (GB/T 33190, the Chinese fixed-layout format used for e-invoices and official documents): extract text and invoice fields, and use them from AI tools.
|
|
32
|
+
|
|
33
|
+
> 状态:0.1 版,接口以后可能还会调整。更新记录见 [CHANGELOG](https://github.com/Edgar-311/ofdkit/blob/main/CHANGELOG.md)。
|
|
34
|
+
> Status: 0.1, the API may still change. See the [CHANGELOG](https://github.com/Edgar-311/ofdkit/blob/main/CHANGELOG.md).
|
|
35
|
+
|
|
36
|
+
## 能做什么 / What it does
|
|
37
|
+
|
|
38
|
+
- 按阅读顺序提取文字,包括模板和复合对象里的文字。
|
|
39
|
+
Text in reading order, including text in templates and composite objects.
|
|
40
|
+
- 读取电子发票字段(数电票和增值税电子发票),输出 JSON:发票号码、开票日期、购买方、销售方、金额、税额、价税合计、明细。
|
|
41
|
+
Invoice fields from e-invoices as JSON: number, date, buyer, seller, amounts, tax, total, line items.
|
|
42
|
+
- 把一个文件夹里的发票汇总成 CSV,Excel 可以直接打开,并提示重复的发票号码。
|
|
43
|
+
A folder of invoices summarised as CSV (opens in Excel), with duplicate invoice numbers flagged.
|
|
44
|
+
- 只用 Python 标准库,没有任何第三方依赖;能读很多不规范的文件(路径写法不对、编码不对、XML 前缀错误等)。
|
|
45
|
+
Standard library only, no dependencies; reads many non-conforming files (bad paths, encodings, XML prefixes).
|
|
46
|
+
|
|
47
|
+
## 安装 / Install
|
|
48
|
+
|
|
49
|
+
需要 Python 3.10 或更新版本。Requires Python 3.10+.
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
pip install ofdkit
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 命令行 / Command line
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
ofdkit text 文件.ofd # 提取文字 / print text
|
|
59
|
+
ofdkit invoice 发票.ofd # 发票字段(JSON)/ invoice fields as JSON
|
|
60
|
+
ofdkit batch 发票文件夹/ -o 发票汇总.csv # 批量汇总 / summarise a folder as CSV
|
|
61
|
+
ofdkit info 文件.ofd # 页数、作者等信息 / pages and metadata
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
`ofdkit invoice` 的输出示例(数据是虚构的)/ Example output (fictional data):
|
|
65
|
+
|
|
66
|
+
```json
|
|
67
|
+
{
|
|
68
|
+
"invoice_number": "00000000000000000001",
|
|
69
|
+
"invoice_code": null,
|
|
70
|
+
"issue_date": "2026-01-02",
|
|
71
|
+
"check_code": null,
|
|
72
|
+
"machine_number": null,
|
|
73
|
+
"buyer": {"name": "测试单位甲", "tax_id": "TESTBUYER0001", "address_phone": null, "bank_account": null},
|
|
74
|
+
"seller": {"name": "测试单位乙", "tax_id": "TESTSELLER0002", "address_phone": null, "bank_account": null},
|
|
75
|
+
"amount_without_tax": "100.00",
|
|
76
|
+
"tax_amount": "6.00",
|
|
77
|
+
"total_amount": "106.00",
|
|
78
|
+
"issuer": "测试人员",
|
|
79
|
+
"payee": null,
|
|
80
|
+
"reviewer": null,
|
|
81
|
+
"note": null,
|
|
82
|
+
"items": [
|
|
83
|
+
{"name": "*测试*服务项目", "specification": null, "unit": null, "quantity": null,
|
|
84
|
+
"unit_price": null, "amount": "100.00", "tax_rate": "6%", "tax_amount": "6.00"}
|
|
85
|
+
],
|
|
86
|
+
"sources": ["custom_tags"],
|
|
87
|
+
"warnings": []
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
金额是字符串,不会丢失精度。如果"合计金额 + 合计税额"不等于"价税合计",`warnings` 里会提示。
|
|
92
|
+
Amounts are decimal strings, so no precision is lost. `warnings` notes totals that do not add up.
|
|
93
|
+
|
|
94
|
+
## 在 AI 工具里使用 / Use from AI tools
|
|
95
|
+
|
|
96
|
+
### MCP 服务器 / MCP server
|
|
97
|
+
|
|
98
|
+
让 Claude、Codex、Cursor 等 AI 工具直接读取 OFD 文件。需要先安装 [uv](https://docs.astral.sh/uv/)。所有工具都是只读的。
|
|
99
|
+
Lets Claude, Codex, Cursor and other AI tools read OFD files. Requires [uv](https://docs.astral.sh/uv/). All tools are read-only.
|
|
100
|
+
|
|
101
|
+
| 工具 / Tool | 作用 / What it does |
|
|
102
|
+
| --- | --- |
|
|
103
|
+
| `read_ofd_text` | 按阅读顺序提取文字,可选 Markdown 和页码范围 / text in reading order, optional Markdown and page range |
|
|
104
|
+
| `read_ofd_invoice` | 发票字段 / invoice fields |
|
|
105
|
+
| `ofd_info` | 页数、尺寸、元数据 / pages, sizes, metadata |
|
|
106
|
+
| `list_ofd_invoices` | 汇总一个文件夹里的发票 / summarise a folder of invoices |
|
|
107
|
+
|
|
108
|
+
Claude Code:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
claude mcp add ofdkit -- uvx --from "ofdkit[mcp]" ofdkit mcp
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
Claude Desktop、Cursor 等使用 JSON 配置的工具 / tools with a JSON config:
|
|
115
|
+
|
|
116
|
+
```json
|
|
117
|
+
{
|
|
118
|
+
"mcpServers": {
|
|
119
|
+
"ofdkit": {
|
|
120
|
+
"command": "uvx",
|
|
121
|
+
"args": ["--from", "ofdkit[mcp]", "ofdkit", "mcp"]
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Codex(`~/.codex/config.toml`):
|
|
128
|
+
|
|
129
|
+
```toml
|
|
130
|
+
[mcp_servers.ofdkit]
|
|
131
|
+
command = "uvx"
|
|
132
|
+
args = ["--from", "ofdkit[mcp]", "ofdkit", "mcp"]
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
加上 `--root 文件夹` 可以只允许读取这个文件夹里的文件。
|
|
136
|
+
Add `--root FOLDER` to allow only files inside that folder.
|
|
137
|
+
|
|
138
|
+
### Agent Skill
|
|
139
|
+
|
|
140
|
+
[`skills/ofd-reader`](https://github.com/Edgar-311/ofdkit/tree/main/skills/ofd-reader) 告诉 AI 什么时候、怎么用 ofdkit 命令行,适用于支持 [Agent Skills](https://agentskills.io) 的工具。把这个文件夹复制到:
|
|
141
|
+
[`skills/ofd-reader`](https://github.com/Edgar-311/ofdkit/tree/main/skills/ofd-reader) tells an AI agent when and how to use the ofdkit command line. Copy the folder to:
|
|
142
|
+
|
|
143
|
+
- Claude Code: `~/.claude/skills/ofd-reader/`
|
|
144
|
+
- Codex: `~/.codex/skills/ofd-reader/`
|
|
145
|
+
|
|
146
|
+
### MarkItDown 插件 / MarkItDown plugin
|
|
147
|
+
|
|
148
|
+
[MarkItDown](https://github.com/microsoft/markitdown) 是把各种文件转成 Markdown 给 AI 读的工具。安装插件后它就能读 OFD:
|
|
149
|
+
[MarkItDown](https://github.com/microsoft/markitdown) converts files to Markdown for AI. With this plugin it reads OFD too:
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
pip install "ofdkit[markitdown]"
|
|
153
|
+
markitdown --use-plugins 发票.ofd
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Python
|
|
157
|
+
|
|
158
|
+
```python
|
|
159
|
+
import ofdkit
|
|
160
|
+
|
|
161
|
+
with ofdkit.open("发票.ofd") as doc:
|
|
162
|
+
print(doc.text())
|
|
163
|
+
invoice = doc.invoice() # 不是发票时返回 None / None if not an invoice
|
|
164
|
+
if invoice:
|
|
165
|
+
print(invoice.invoice_number, invoice.total_amount)
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
## 注意 / Notes
|
|
169
|
+
|
|
170
|
+
- ofdkit 只读取文件里写了什么,不验证发票真伪。查验真伪请使用国家税务总局的发票查验平台。
|
|
171
|
+
ofdkit reports what a file contains. It does not verify that an invoice is genuine.
|
|
172
|
+
- 文字如果是画成图形或图片的,就读不出来(需要 OCR,不在本项目范围内)。
|
|
173
|
+
Text drawn as shapes or images cannot be extracted (that needs OCR, which is out of scope).
|
|
174
|
+
- 多栏排版的页面,目前两栏的文字会出现在同一行,用三个空格隔开。
|
|
175
|
+
On multi-column pages, both columns currently share each line, separated by three spaces.
|
|
176
|
+
|
|
177
|
+
## 原则 / Principles
|
|
178
|
+
|
|
179
|
+
- 只读取 OFD,不生成、不修改发票。
|
|
180
|
+
Reads OFD only. It never creates or modifies invoices.
|
|
181
|
+
- 本项目大部分代码由 AI(Claude)编写,维护者负责决定方向和批准发布。
|
|
182
|
+
Most code is written with AI assistance (Claude). The maintainer sets the direction and approves releases.
|
|
183
|
+
|
|
184
|
+
## 许可证 / License
|
|
185
|
+
|
|
186
|
+
[Apache-2.0](https://github.com/Edgar-311/ofdkit/blob/main/LICENSE)
|
ofdkit-0.1.0/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# ofdkit
|
|
2
|
+
|
|
3
|
+
轻量的 Python 工具,用来读取 OFD 文件,提取里面的文字和发票字段,并能接入 AI 工具。OFD 是中国的国家标准版式文档格式(GB/T 33190),常见于电子发票和公文。
|
|
4
|
+
|
|
5
|
+
A lightweight Python toolkit for reading OFD documents (GB/T 33190, the Chinese fixed-layout format used for e-invoices and official documents): extract text and invoice fields, and use them from AI tools.
|
|
6
|
+
|
|
7
|
+
> 状态:0.1 版,接口以后可能还会调整。更新记录见 [CHANGELOG](https://github.com/Edgar-311/ofdkit/blob/main/CHANGELOG.md)。
|
|
8
|
+
> Status: 0.1, the API may still change. See the [CHANGELOG](https://github.com/Edgar-311/ofdkit/blob/main/CHANGELOG.md).
|
|
9
|
+
|
|
10
|
+
## 能做什么 / What it does
|
|
11
|
+
|
|
12
|
+
- 按阅读顺序提取文字,包括模板和复合对象里的文字。
|
|
13
|
+
Text in reading order, including text in templates and composite objects.
|
|
14
|
+
- 读取电子发票字段(数电票和增值税电子发票),输出 JSON:发票号码、开票日期、购买方、销售方、金额、税额、价税合计、明细。
|
|
15
|
+
Invoice fields from e-invoices as JSON: number, date, buyer, seller, amounts, tax, total, line items.
|
|
16
|
+
- 把一个文件夹里的发票汇总成 CSV,Excel 可以直接打开,并提示重复的发票号码。
|
|
17
|
+
A folder of invoices summarised as CSV (opens in Excel), with duplicate invoice numbers flagged.
|
|
18
|
+
- 只用 Python 标准库,没有任何第三方依赖;能读很多不规范的文件(路径写法不对、编码不对、XML 前缀错误等)。
|
|
19
|
+
Standard library only, no dependencies; reads many non-conforming files (bad paths, encodings, XML prefixes).
|
|
20
|
+
|
|
21
|
+
## 安装 / Install
|
|
22
|
+
|
|
23
|
+
需要 Python 3.10 或更新版本。Requires Python 3.10+.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install ofdkit
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 命令行 / Command line
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
ofdkit text 文件.ofd # 提取文字 / print text
|
|
33
|
+
ofdkit invoice 发票.ofd # 发票字段(JSON)/ invoice fields as JSON
|
|
34
|
+
ofdkit batch 发票文件夹/ -o 发票汇总.csv # 批量汇总 / summarise a folder as CSV
|
|
35
|
+
ofdkit info 文件.ofd # 页数、作者等信息 / pages and metadata
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`ofdkit invoice` 的输出示例(数据是虚构的)/ Example output (fictional data):
|
|
39
|
+
|
|
40
|
+
```json
|
|
41
|
+
{
|
|
42
|
+
"invoice_number": "00000000000000000001",
|
|
43
|
+
"invoice_code": null,
|
|
44
|
+
"issue_date": "2026-01-02",
|
|
45
|
+
"check_code": null,
|
|
46
|
+
"machine_number": null,
|
|
47
|
+
"buyer": {"name": "测试单位甲", "tax_id": "TESTBUYER0001", "address_phone": null, "bank_account": null},
|
|
48
|
+
"seller": {"name": "测试单位乙", "tax_id": "TESTSELLER0002", "address_phone": null, "bank_account": null},
|
|
49
|
+
"amount_without_tax": "100.00",
|
|
50
|
+
"tax_amount": "6.00",
|
|
51
|
+
"total_amount": "106.00",
|
|
52
|
+
"issuer": "测试人员",
|
|
53
|
+
"payee": null,
|
|
54
|
+
"reviewer": null,
|
|
55
|
+
"note": null,
|
|
56
|
+
"items": [
|
|
57
|
+
{"name": "*测试*服务项目", "specification": null, "unit": null, "quantity": null,
|
|
58
|
+
"unit_price": null, "amount": "100.00", "tax_rate": "6%", "tax_amount": "6.00"}
|
|
59
|
+
],
|
|
60
|
+
"sources": ["custom_tags"],
|
|
61
|
+
"warnings": []
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
金额是字符串,不会丢失精度。如果"合计金额 + 合计税额"不等于"价税合计",`warnings` 里会提示。
|
|
66
|
+
Amounts are decimal strings, so no precision is lost. `warnings` notes totals that do not add up.
|
|
67
|
+
|
|
68
|
+
## 在 AI 工具里使用 / Use from AI tools
|
|
69
|
+
|
|
70
|
+
### MCP 服务器 / MCP server
|
|
71
|
+
|
|
72
|
+
让 Claude、Codex、Cursor 等 AI 工具直接读取 OFD 文件。需要先安装 [uv](https://docs.astral.sh/uv/)。所有工具都是只读的。
|
|
73
|
+
Lets Claude, Codex, Cursor and other AI tools read OFD files. Requires [uv](https://docs.astral.sh/uv/). All tools are read-only.
|
|
74
|
+
|
|
75
|
+
| 工具 / Tool | 作用 / What it does |
|
|
76
|
+
| --- | --- |
|
|
77
|
+
| `read_ofd_text` | 按阅读顺序提取文字,可选 Markdown 和页码范围 / text in reading order, optional Markdown and page range |
|
|
78
|
+
| `read_ofd_invoice` | 发票字段 / invoice fields |
|
|
79
|
+
| `ofd_info` | 页数、尺寸、元数据 / pages, sizes, metadata |
|
|
80
|
+
| `list_ofd_invoices` | 汇总一个文件夹里的发票 / summarise a folder of invoices |
|
|
81
|
+
|
|
82
|
+
Claude Code:
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
claude mcp add ofdkit -- uvx --from "ofdkit[mcp]" ofdkit mcp
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Claude Desktop、Cursor 等使用 JSON 配置的工具 / tools with a JSON config:
|
|
89
|
+
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"mcpServers": {
|
|
93
|
+
"ofdkit": {
|
|
94
|
+
"command": "uvx",
|
|
95
|
+
"args": ["--from", "ofdkit[mcp]", "ofdkit", "mcp"]
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
Codex(`~/.codex/config.toml`):
|
|
102
|
+
|
|
103
|
+
```toml
|
|
104
|
+
[mcp_servers.ofdkit]
|
|
105
|
+
command = "uvx"
|
|
106
|
+
args = ["--from", "ofdkit[mcp]", "ofdkit", "mcp"]
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
加上 `--root 文件夹` 可以只允许读取这个文件夹里的文件。
|
|
110
|
+
Add `--root FOLDER` to allow only files inside that folder.
|
|
111
|
+
|
|
112
|
+
### Agent Skill
|
|
113
|
+
|
|
114
|
+
[`skills/ofd-reader`](https://github.com/Edgar-311/ofdkit/tree/main/skills/ofd-reader) 告诉 AI 什么时候、怎么用 ofdkit 命令行,适用于支持 [Agent Skills](https://agentskills.io) 的工具。把这个文件夹复制到:
|
|
115
|
+
[`skills/ofd-reader`](https://github.com/Edgar-311/ofdkit/tree/main/skills/ofd-reader) tells an AI agent when and how to use the ofdkit command line. Copy the folder to:
|
|
116
|
+
|
|
117
|
+
- Claude Code: `~/.claude/skills/ofd-reader/`
|
|
118
|
+
- Codex: `~/.codex/skills/ofd-reader/`
|
|
119
|
+
|
|
120
|
+
### MarkItDown 插件 / MarkItDown plugin
|
|
121
|
+
|
|
122
|
+
[MarkItDown](https://github.com/microsoft/markitdown) 是把各种文件转成 Markdown 给 AI 读的工具。安装插件后它就能读 OFD:
|
|
123
|
+
[MarkItDown](https://github.com/microsoft/markitdown) converts files to Markdown for AI. With this plugin it reads OFD too:
|
|
124
|
+
|
|
125
|
+
```bash
|
|
126
|
+
pip install "ofdkit[markitdown]"
|
|
127
|
+
markitdown --use-plugins 发票.ofd
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## Python
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
import ofdkit
|
|
134
|
+
|
|
135
|
+
with ofdkit.open("发票.ofd") as doc:
|
|
136
|
+
print(doc.text())
|
|
137
|
+
invoice = doc.invoice() # 不是发票时返回 None / None if not an invoice
|
|
138
|
+
if invoice:
|
|
139
|
+
print(invoice.invoice_number, invoice.total_amount)
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## 注意 / Notes
|
|
143
|
+
|
|
144
|
+
- ofdkit 只读取文件里写了什么,不验证发票真伪。查验真伪请使用国家税务总局的发票查验平台。
|
|
145
|
+
ofdkit reports what a file contains. It does not verify that an invoice is genuine.
|
|
146
|
+
- 文字如果是画成图形或图片的,就读不出来(需要 OCR,不在本项目范围内)。
|
|
147
|
+
Text drawn as shapes or images cannot be extracted (that needs OCR, which is out of scope).
|
|
148
|
+
- 多栏排版的页面,目前两栏的文字会出现在同一行,用三个空格隔开。
|
|
149
|
+
On multi-column pages, both columns currently share each line, separated by three spaces.
|
|
150
|
+
|
|
151
|
+
## 原则 / Principles
|
|
152
|
+
|
|
153
|
+
- 只读取 OFD,不生成、不修改发票。
|
|
154
|
+
Reads OFD only. It never creates or modifies invoices.
|
|
155
|
+
- 本项目大部分代码由 AI(Claude)编写,维护者负责决定方向和批准发布。
|
|
156
|
+
Most code is written with AI assistance (Claude). The maintainer sets the direction and approves releases.
|
|
157
|
+
|
|
158
|
+
## 许可证 / License
|
|
159
|
+
|
|
160
|
+
[Apache-2.0](https://github.com/Edgar-311/ofdkit/blob/main/LICENSE)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.11.32,<0.12"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ofdkit"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Read OFD documents (GB/T 33190) and Chinese e-invoices: text, invoice fields, and AI tool integrations."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "Apache-2.0"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
requires-python = ">=3.10"
|
|
13
|
+
maintainers = [{ name = "Edgar-311" }]
|
|
14
|
+
keywords = ["ofd", "gb/t 33190", "e-invoice", "fapiao", "电子发票", "数电票", "mcp"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 3 - Alpha",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"Natural Language :: Chinese (Simplified)",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Topic :: Office/Business :: Financial :: Accounting",
|
|
23
|
+
"Topic :: Text Processing",
|
|
24
|
+
]
|
|
25
|
+
dependencies = []
|
|
26
|
+
|
|
27
|
+
[project.optional-dependencies]
|
|
28
|
+
markitdown = ["markitdown>=0.1,<0.2"]
|
|
29
|
+
mcp = ["mcp>=2.3,<3"]
|
|
30
|
+
|
|
31
|
+
[project.scripts]
|
|
32
|
+
ofdkit = "ofdkit.cli:main"
|
|
33
|
+
|
|
34
|
+
[project.entry-points."markitdown.plugin"]
|
|
35
|
+
ofdkit = "ofdkit.markitdown_plugin"
|
|
36
|
+
|
|
37
|
+
[project.urls]
|
|
38
|
+
Homepage = "https://github.com/Edgar-311/ofdkit"
|
|
39
|
+
Issues = "https://github.com/Edgar-311/ofdkit/issues"
|
|
40
|
+
Changelog = "https://github.com/Edgar-311/ofdkit/blob/main/CHANGELOG.md"
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""ofdkit: read OFD documents (GB/T 33190) and Chinese e-invoices.
|
|
2
|
+
|
|
3
|
+
>>> import ofdkit
|
|
4
|
+
>>> with ofdkit.open("invoice.ofd") as doc: # doctest: +SKIP
|
|
5
|
+
... print(doc.text())
|
|
6
|
+
... print(doc.invoice())
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from importlib.metadata import PackageNotFoundError
|
|
10
|
+
from importlib.metadata import version as _version
|
|
11
|
+
|
|
12
|
+
from .document import DocInfo, OFDDocument, Page, TextRun
|
|
13
|
+
from .errors import OFDError, OFDFormatError, OFDLimitError
|
|
14
|
+
from .invoice import Invoice, LineItem, Party
|
|
15
|
+
from .package import Source
|
|
16
|
+
|
|
17
|
+
try:
|
|
18
|
+
__version__ = _version("ofdkit")
|
|
19
|
+
except PackageNotFoundError: # running from a source checkout that is not installed
|
|
20
|
+
__version__ = "0.0.0+unknown"
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"DocInfo",
|
|
24
|
+
"Invoice",
|
|
25
|
+
"LineItem",
|
|
26
|
+
"OFDDocument",
|
|
27
|
+
"OFDError",
|
|
28
|
+
"OFDFormatError",
|
|
29
|
+
"OFDLimitError",
|
|
30
|
+
"Page",
|
|
31
|
+
"Party",
|
|
32
|
+
"TextRun",
|
|
33
|
+
"__version__",
|
|
34
|
+
"extract_invoice",
|
|
35
|
+
"extract_text",
|
|
36
|
+
"open",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def open(source: Source) -> OFDDocument: # noqa: A001 - mirrors builtins.open on purpose
|
|
41
|
+
"""Open an OFD file from a path, bytes, or a binary file object."""
|
|
42
|
+
return OFDDocument(source)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def extract_text(source: Source, page_separator: str = "\n\n") -> str:
|
|
46
|
+
"""Return all text of an OFD file in reading order."""
|
|
47
|
+
with OFDDocument(source) as document:
|
|
48
|
+
return document.text(page_separator)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def extract_invoice(source: Source) -> Invoice | None:
|
|
52
|
+
"""Return the invoice data of an OFD e-invoice, or None if the file holds none."""
|
|
53
|
+
with OFDDocument(source) as document:
|
|
54
|
+
return document.invoice()
|