Skip to main content

opencc-py

CI PyPI License: MIT

opencc-py 是依照 Will 保哥的 C# OpenCC 實作移植的純 Python OpenCC 函式庫。核心行為保留原實作的內嵌詞庫、locale/preset、Trie 最長匹配與多階段轉換流程。

範例

請參考 examples/,內含基本轉換、不套用詞彙轉換、不同簡繁詞庫差異、自訂詞典與 HTML/XML 轉換範例。

功能

  • 純 Python 3.11+,無執行階段相依套件。
  • 內建 cn、hk、tw、tw2、twp、jp locale。
  • 支援 full、cn2t、t2cn preset。
  • 支援自訂字典與多個字典群組串接。
  • Unicode code point 層級的 Trie 最長匹配。
  • XML 相容 HTML 轉換與還原。
  • 提供 opencc-py CLI。

安裝

pip install opencc-py-tw2

基本用法

from opencc_py import converter

convert = converter("cn", "tw2")
print(convert("汉语"))  # 漢語
print(convert.convert("默认用户界面支持数据库和网络请求。"))

Locale 與 preset

預設 opencc_py.converter(from_, to) 使用 full preset:

locale 說明
cn 中國大陸簡體
hk 香港繁體異體字
tw 台灣繁體異體字
tw2 台灣繁體常用詞
twp 台灣繁體含 IT、姓名與其他詞彙
jp 日本新字體/異體字
t 直接通過,不載入該階段字典

tw2 的「台/臺」規則:日常用語用「台」(台北、舞台、電視台),「颱」「檯」不變(颱風、櫃檯),正式名稱保留「臺」(國立臺灣大學、臺北市政府、新臺幣)。輸入寫成「台」的正式名稱也會統一成「臺」。tw2 轉回其他地區時不會把「台」改回「臺」,所以 tw → tw2 → tw 不保證能還原原文。白名單來源是 opencc-js 的 src/custom/TW2TaiProperNouns.js,字典資料是 TW2Tai/TW2TaiRev,用 opencc-js 的 tools/gen-tw2-tai.mjs 產生。

方向限定 preset:

from opencc_py import presets

cn_to_tw = presets.cn2t.converter("cn", "tw2")
tw_to_cn = presets.t2cn.converter("tw2", "cn")

自訂字典

字串格式與 C# 實作相同:每筆 來源 目標,筆與筆之間以 | 分隔;若詞條內含空白,可用 tab 分隔來源與目標。

from opencc_py import DictEntry, custom_converter

convert = custom_converter("香蕉 banana|蘋果 apple|Web 平台庫\tWeb 平台函式庫")
print(convert("香蕉 蘋果 Web 平台庫"))

convert2 = custom_converter([
    DictEntry("“", "「"),
    DictEntry("”", "」"),
])

進階組合

converter_factory() 會依序套用每個 DictGroup,等同 C# 版本先處理 from 群組,再處理 to 群組。

from opencc_py import DictEntry, DictGroup, converter_factory

first = DictGroup.from_entries([DictEntry("a", "b")])
second = DictGroup.from_entries([DictEntry("b", "c")])
convert = converter_factory(first, second)
print(convert("a"))  # c

XML 相容 HTML 轉換

此功能使用 Python 標準函式庫 xml.etree.ElementTree,適用於可被 XML parser 解析的 HTML/XML。它會轉換符合 lang 範圍內的文字、meta[name=description|keywords] 的 content、img alt、input[type=button] value,並略過 script、style 與 ignore-opencc class。

from opencc_py import HtmlConverter, converter

convert = converter("hk", "cn")
html = HtmlConverter.from_xml_string(
    convert,
    "<html lang='zh-HK'><body><p lang='zh-HK'>漢語</p></body></html>",
    "zh-HK",
    "zh-CN",
)
html.convert()
print(html.to_xml_string())
html.restore()

CLI

opencc-py input.txt cn tw2
opencc-py input.txt cn tw2 -o output.txt
opencc-py input.txt cn tw2 --in-place

-o/--output 與 -i/--in-place 不能同時使用。輸入與輸出皆使用 UTF-8。

範例

更多可執行範例請見 samples/。

開發

python -m venv .venv
source .venv/bin/activate
python -m unittest discover -s tests
python -m pip install build
python -m build

如果 C# OpenCC 原始碼與本專案位於同一個父目錄,可重新產生內嵌詞庫:

python tools/generate_dict_data.py
python -m unittest discover -s tests

發布

PyPI 發布流程請見 PUBLISHING.md。

授權

MIT

台灣詞彙品質改善

tw2 / twp 的共用詞彙規則、語境限制、測試結果與效能對照,請見 台灣詞彙品質說明。

Metadata

Release files for opencc-py-tw2 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for opencc-py-tw2 0.1.2
File Size Uploaded Provenance Actions
opencc_py_tw2-0.1.2.tar.gz 501.0 kB With provenance Details for opencc_py_tw2-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for opencc-py-tw2 0.1.2
File Interpreter ABI Platform Actions
opencc_py_tw2-0.1.2-py3-none-any.whl Python 3 none any Details for opencc_py_tw2-0.1.2-py3-none-any.whl

Total release size: 989.1 kB

Release files / opencc_py_tw2-0.1.2.tar.gz

Download URL opencc_py_tw2-0.1.2.tar.gz
Size 501.0 kB
Tags Source
SHA-256 checksum
How to use checksums
484e9b848bf9d67e295c82fd9c4d9b32c86c13a4bc1a240f3770ff485ad45a94
BLAKE2b-256 checksum
How to use checksums
2f129b589635d48ed3c00904caa59bc7ffda52d4bde18f98b9322713fd7d755d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Release files / opencc_py_tw2-0.1.2-py3-none-any.whl

Download URL opencc_py_tw2-0.1.2-py3-none-any.whl
Size 488.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a5a402559b7437dfc670bc1f45c692defbb165e40507b6798aeccda303f83603
BLAKE2b-256 checksum
How to use checksums
c2ab300a864ac2f954c71fd0045f76d4f2fc93d21436ec7218133e68140ab963
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

Security for opencc-py-tw2 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for opencc_py_tw2-0.1.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 21, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page