{"name":"python-pinyin: A Robust Python Library for Hanzi to Pinyin Conversion","description":"`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.","github":"https://github.com/mozillazg/python-pinyin","url":"https://osrepos.com/repo/mozillazg-python-pinyin","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/mozillazg-python-pinyin","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/mozillazg-python-pinyin.md","json":"https://osrepos.com/repo/mozillazg-python-pinyin.json","topics":["python","pypinyin","chinese","hanzi","pinyin","Natural Language Processing","Utilities"],"keywords":["python","pypinyin","chinese","hanzi","pinyin","Natural Language Processing","Utilities"],"stars":null,"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.","content":"## Introduction\n\n`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.\n\n## Installation\n\nYou can easily install `python-pinyin` using pip:\n\nbash\npip install pypinyin\n\n\nAlternatively, if you use `uv`, you can install it with:\n\nbash\nuv add pypinyin\n\n\n## Examples\n\nHere are some examples demonstrating the versatility of `python-pinyin`:\n\npython\nfrom pypinyin import pinyin, lazy_pinyin, Style\n\n# Basic conversion\nprint(pinyin('??'))\n# Output: [['zh?ng'], ['x?n']]\n\n# Enable polyphonic character mode\nprint(pinyin('??', heteronym=True))\n# Output: [['zh?ng', 'zhòng'], ['x?n']]\n\n# Set Pinyin style (e.g., first letter)\nprint(pinyin('??', style=Style.FIRST_LETTER))\n# Output: [['z'], ['x']]\n\n# Bopomofo style\nprint(pinyin('??', style=Style.BOPOMOFO))\n# Output: [['???'], ['???']]\n\n# Lazy Pinyin (no tone marks, single result per character)\nprint(lazy_pinyin('??'))\n# Output: ['zhong', 'xin']\n\n\n## Why Use `python-pinyin`?\n\n`python-pinyin` offers several compelling reasons for developers working with Chinese text:\n\n*   **Accuracy**: It intelligently matches the most correct Pinyin based on phrases and provides comprehensive support for polyphonic characters, ensuring high accuracy in conversions.\n*   **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.\n*   **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.\n*   **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.\n*   **Robust and Maintained**: Developed by mozillazg, the project benefits from active maintenance and a strong community, ensuring reliability and ongoing improvements.\n\n## Links\n\n*   Official Documentation: [https://pypinyin.readthedocs.io/](https://pypinyin.readthedocs.io/)\n*   GitHub Repository: [https://github.com/mozillazg/python-pinyin](https://github.com/mozillazg/python-pinyin)\n*   PyPI Project Page: [https://pypi.org/project/pypinyin](https://pypi.org/project/pypinyin)","metrics":{"detailViews":4,"githubClicks":0},"dates":{"published":null,"modified":"2026-08-01T00:59:50.000Z"}}