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.
Model Mommy: A Legacy Python Fixture Factory, Migrate to Model Bakery
August 2, 2026
Model Mommy was a popular Python library designed to simplify the creation of smart test fixtures and realistic test data. This project is no longer actively maintained, and its users are strongly advised to migrate to its successor, Model Bakery. The renaming to Model Bakery was a conscious decision to avoid reinforcing gender stereotypes within the technology community.

Faker: Generate Realistic Fake Data for Your Python Projects
August 2, 2026
Faker is a powerful Python package designed to generate realistic fake data. It's an essential tool for bootstrapping databases, creating test data, filling persistence layers for stress testing, or anonymizing sensitive production data. With support for various data types and localization, Faker streamlines development and testing workflows.

pangu.py: Automated Paranoid Text Spacing for Enhanced Readability
August 1, 2026
pangu.py is a Python library designed for opinionated, "paranoid" text spacing. It automatically inserts whitespace between CJK (Chinese, Japanese, Korean) and half-width characters, such as alphabetical letters, numerical digits, and symbols. This tool significantly improves text readability by ensuring consistent and proper spacing.
pyfiglet: Render ASCII Text into ASCII Art Fonts in Python
August 1, 2026
pyfiglet is a pure Python port of the classic FIGlet utility, designed to transform ordinary ASCII text into impressive ASCII art fonts. It offers flexibility, allowing users to generate stylized text both from the command line and directly within their Python applications. This tool is ideal for adding unique visual flair to terminal outputs, banners, or creative text displays.
Source repository
Open the original repository on GitHub.