Skip to main content

一个简单、灵活的库,用于在终端模拟打字机输出效果。

Project description

Py-Typewriter

PyPI version

一个简单、灵活的 Python 库,用于在终端模拟自然的打字机输出效果。

特点

  • 简单易用: 只需一行代码即可实现打字机效果。
  • 自然模拟: 通过随机延迟和标点停顿,效果更逼真。
  • 高度灵活: 提供底层生成器,允许你完全自定义输出行为。

安装

pip install py-typewriter

使用方法

快速上手

最简单的方式是使用 typewrite() 函数:

import typewriter

story = "很久很久以前,在一个遥远的国度里...\n一切都显得那么宁静。"
typewriter.typewrite(story)

高级用法 (使用生成器)

如果是更灵活的调用,可以使用 generate_typewriter_flow() 生成器:

import time
import typewriter

text = "这是一个更高级的用法。"

flow = typewriter.generate_typewriter_flow(text, base_delay=0.1)

for char, delay in flow:
    print(char, end='', flush=True)
    time.sleep(delay)
print()

支持 char(Default)、word模式,word模式支持对中文进行分词,更贴近实际效果

    text_sample_cn = "你好,世界!这是一个基于Jieba分词的打字机效果模拟。它能让中文输出更自然、流畅。"
    text_sample_en = "Hello, world! This is a typewriter effect simulation."

    print("--- 模式: 'char' (默认字符模式) ---")
    flow_char = generate_typewriter_flow(text_sample_cn, base_delay=0.03)
    for char, delay in flow_char:
        print(char, end="", flush=True)  # flush=True 确保立即输出
        time.sleep(delay)
    print("\n")  # 换行

    print("--- 模式: 'word' (Jieba分词模式) ---")
    try:
        flow_word = generate_typewriter_flow(text_sample_cn, base_delay=0.03, mode="word")
        for word, delay in flow_word:
            print(word, end="", flush=True)
            time.sleep(delay)
        print("\n")
    except ImportError as e:
        print(f"\n错误: {e}")

    print("--- 英文文本在 'word' 模式下的效果 ---")
    # Jieba 也能很好地处理英文和数字
    try:
        flow_en_word = generate_typewriter_flow(text_sample_en, base_delay=0.03, mode="word")
        for word, delay in flow_en_word:
            print(word, end="", flush=True)
            time.sleep(delay)
        print("\n")
    except ImportError as e:
        print(f"\n错误: {e}")

贡献

欢迎提交 Issues 和 Pull Requests!

许可证

本项目使用 MIT License

4. 许可证 (LICENSE)

# LICENSE

MIT License

Copyright (c) 2025 orxvan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_typewriter_sse-0.2.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_typewriter_sse-0.2.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file py_typewriter_sse-0.2.0.tar.gz.

File metadata

  • Download URL: py_typewriter_sse-0.2.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for py_typewriter_sse-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e09a35631a763fb8fad4d3726995094bf7b3b4993568f30f8bafe3002c85d284
MD5 b40ce4d6970c403c688052b21d5555fc
BLAKE2b-256 4863292dc56a827228372eb986b3730722a822884f7a91b86e4e56c469314320

See more details on using hashes here.

File details

Details for the file py_typewriter_sse-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for py_typewriter_sse-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 54b45641741d49c3d11c31c0e9075c2df966ab3fe9c819cc3059eb87eb7c93e3
MD5 a3d10949f5138ee6611b4cd4436d0670
BLAKE2b-256 89ede0ae60e2583f782de32bebf12f4184242cb43c3db8cc977564383c4e2c08

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page