Skip to main content

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

Project description

Py-Typewriter

PyPI version

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

特点

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

安装

pip install py-typewriter-sse

使用方法

快速上手

最简单的方式是使用 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 = typewriter.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 = typewriter.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_word = typewriter.generate_typewriter_flow(
            text_sample_cn, base_delay=0.03, mode="word", max_chunk_size=1, min_chunk_size=1
        )
        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 = typewriter.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.3.0.tar.gz (5.5 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.3.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: py_typewriter_sse-0.3.0.tar.gz
  • Upload date:
  • Size: 5.5 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.3.0.tar.gz
Algorithm Hash digest
SHA256 ed7d645c5cb970e7d20a8917eb9d8e7892a2c847af27fcbf6b68c587256ad533
MD5 0eafc2ba7f17919de13c7f5ebbf26a73
BLAKE2b-256 eca8a9cff95de3fabeb3a75a42c466e39f9b4dfcd52d970af906af9f5cd2508f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for py_typewriter_sse-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 827e5c85dc44418597fe7ce6c935328e8c302379362879813cf0fb6343ca6872
MD5 09c4a6e8306bfe3cc2c58798266e84f9
BLAKE2b-256 ebfe25386eb6d47d5d03c7997ed90757e9246eea2b8ddba37ba3d08454b9ab90

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