python-pinyin: A Robust Python Library for Hanzi to Pinyin Conversion
This repository profile is provided by osrepos.com, an open source repository discovery platform.
Summary
`python-pinyin` is a powerful Python library designed for converting Chinese characters (Hanzi) into Pinyin. It offers advanced features such as intelligent phrase matching, comprehensive support for polyphonic characters, and various Pinyin and Bopomofo output styles. This tool is essential for developers working on Chinese text processing tasks, including annotation, sorting, and search functionalities.
Repository Information
Use at your own risk
OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.
Introduction
`python-pinyin` is a powerful Python library developed by mozillazg that facilitates the conversion of Chinese characters (Hanzi) into Pinyin. It is widely used for various applications, including annotating Chinese text, sorting data, and enabling efficient search functionalities. The library stands out for its intelligent phrase matching, comprehensive support for polyphonic characters, and flexibility in outputting different Pinyin and Bopomofo styles.
Installation
You can easily install `python-pinyin` using pip:
pip install pypinyin
Alternatively, if you use `uv`, you can install it with:
uv add pypinyin
Examples
Here are some examples demonstrating the versatility of `python-pinyin`:
from pypinyin import pinyin, lazy_pinyin, Style
# Basic conversion
print(pinyin('??'))
# Output: [['zh?ng'], ['x?n']]
# Enable polyphonic character mode
print(pinyin('??', heteronym=True))
# Output: [['zh?ng', 'zhòng'], ['x?n']]
# Set Pinyin style (e.g., first letter)
print(pinyin('??', style=Style.FIRST_LETTER))
# Output: [['z'], ['x']]
# Bopomofo style
print(pinyin('??', style=Style.BOPOMOFO))
# Output: [['???'], ['???']]
# Lazy Pinyin (no tone marks, single result per character)
print(lazy_pinyin('??'))
# Output: ['zhong', 'xin']
Why Use `python-pinyin`?
`python-pinyin` offers several compelling reasons for developers working with Chinese text:
- Accuracy: It intelligently matches the most correct Pinyin based on phrases and provides comprehensive support for polyphonic characters, ensuring high accuracy in conversions.
- Flexibility: The library offers a wide range of Pinyin and Bopomofo styles, including options for tone marks, first letters, and Wade-Giles, catering to diverse linguistic and application requirements.
- Customization: Users can easily customize phrase and single-character Pinyin dictionaries to correct or optimize conversion results. It also integrates seamlessly with external Pinyin data projects for enhanced accuracy.
- Comprehensive Features: Beyond basic conversion, `python-pinyin` includes utilities for Pinyin style conversion, command-line tools, and extensive documentation covering common FAQs and advanced usage scenarios.
- Robust and Maintained: Developed by mozillazg, the project benefits from active maintenance and a strong community, ensuring reliability and ongoing improvements.
Links
- Official Documentation: https://pypinyin.readthedocs.io/
- GitHub Repository: https://github.com/mozillazg/python-pinyin
- PyPI Project Page: https://pypi.org/project/pypinyin
Related repositories
Similar repositories that may be relevant next.

ctx-gate: LLM Context Gateway for Efficient Token Usage
September 16, 2026
ctx-gate is an LLM-agnostic context optimization proxy that reduces token consumption in AI interactions. It intelligently prunes conversation history and tool outputs, ensuring critical facts are retained without altering your workflow. Compatible with Anthropic and OpenAI APIs, ctx-gate helps developers manage LLM costs and maintain prompt fidelity.
Ferret MCP: AI-Powered Knowledge Extraction for Any Codebase
September 14, 2026
Ferret MCP is an MCP server designed to extract comprehensive knowledge from any codebase, combining static analysis with AI-powered deep interpretation. It provides detailed insights into architecture, patterns, dependencies, and API surface, delivering a senior engineer's analysis in seconds. This tool integrates seamlessly with various MCP clients, offering both free static analysis and advanced AI-driven reports.
Local LLM Linux Troubleshoot: An AI Agent for Linux Diagnostics
September 14, 2026
Local LLM Linux Troubleshoot is an AI-powered agent designed to diagnose and resolve issues on Linux systems. It leverages llama.cpp for local AI processing, offering system diagnostics, safe command execution, and support for Docker, CLI, and a web GUI. This tool provides a privacy-first approach to managing and troubleshooting your Linux environment.

OpenWorkProof: Verifiable Work Contracts for AI Agent Systems
September 13, 2026
OpenWorkProof is an open protocol designed to bring transparency and accountability to AI agent work. It establishes verifiable contracts for multi-agent systems, ensuring that tasks are authorized, executed within agreed scopes, and independently verifiable. This protocol addresses critical questions about authorization, execution evidence, and human acceptance in AI-driven workflows.
Source repository
Open the original repository on GitHub.
6 counted GitHub visits