Skip to main content

A word counting tool that handles mixed Asian and non-Asian text like Microsoft Office

Project description

office-wordcount

A word counting tool that handles mixed Asian and non-Asian text, counting words the same way Microsoft Office does.

CI PyPI version Python 3.13+

Features

  • Office-compatible word counting: Counts words like Microsoft Office
    • Non-Asian text: Counts whitespace-separated tokens
    • Asian text (CJK): Counts each character as one word
  • Multiple output formats: JSON (for scripting) and ASCII table (for humans)
  • Pipe-friendly: Automatically uses JSON when piped, table when interactive
  • Type-safe: Full type hints and PEP 561 compliance

Installation

pip install office-wordcount

Or with uv:

uv add office-wordcount

Usage

Command Line

# Count words in a file (full statistics)
office-wordcount document.txt

# Read from stdin
cat document.txt | office-wordcount

# Get single values (useful for scripting)
office-wordcount -w document.txt    # words only
office-wordcount -c document.txt    # characters (no spaces)
office-wordcount -C document.txt    # characters (with spaces)
office-wordcount -a document.txt    # Asian characters only
office-wordcount -A document.txt    # non-Asian words only

# Process multiple files
office-wordcount *.txt

# Force JSON output
office-wordcount --format json document.txt

# Show only total for multiple files
office-wordcount --total *.txt

CLI Options

Option Long Description
-w --words Output only the word count
-c --chars Output only the character count (no spaces)
-C --chars-with-space Output only the character count (with spaces)
-a --asian Output only the Asian character count
-A --non-asian Output only the non-Asian word count
-f --format Output format: json, table, or auto
-t --total Show only the total (for multiple files)

Output Examples

Table format (default for terminal):

Words                  : 150
Characters (no space)  : 750
Characters (with space): 800
Non-Asian words        : 100
Asian characters       : 50

JSON format (default for pipes):

{
  "words": 150,
  "characters_no_space": 750,
  "characters_with_space": 800,
  "non_asian_words": 100,
  "asian_characters": 50
}

Single value (with -w, -c, etc.):

$ office-wordcount -w document.txt
150

Python API

# Simple functions for single values
from word_count import count_words, count_characters

words = count_words("Hello 世界")        # 3
chars = count_characters("Hello 世界")   # 7

# Full statistics
from word_count import calculate_word_statistics

stats = calculate_word_statistics("Hello 世界")
print(stats.words)            # 3 (1 English word + 2 Chinese characters)
print(stats.non_asian_words)  # 1
print(stats.asian_characters) # 2

# Convert to dictionary
print(stats.to_dict())

Available Functions

Function Description
count_words(text) Count total words
count_characters(text) Count characters (no spaces)
count_characters_with_space(text) Count characters (with spaces)
calculate_word_statistics(text) Get all statistics as Statistics object

Why This Tool?

When counting words in documents with mixed languages (like English and Japanese), different tools give different results:

Tool "Hello 世界" Notes
wc -w 2 Splits only by whitespace
Microsoft Word 3 Counts Asian chars individually
office-wordcount 3 Matches Office behavior

This tool is designed to match Microsoft Office's word counting, which is the standard in many professional contexts.

Validation

See validation_dataset.md for test cases used to verify Microsoft Word compatibility. The dataset includes:

  • Basic tests (English, Japanese, Korean, Chinese)
  • Mixed language tests
  • Special characters (fullwidth, circled numbers, era names)
  • CJK radicals and extensions
  • Punctuation handling
  • Emoji tests

Development

# Clone the repository
git clone https://github.com/bebe3/office-wordcount.git
cd office-wordcount

# Install dependencies
uv sync --dev

# Run tests
uv run pytest

# Run linter
uv run ruff check src tests

# Run type checker
uv run mypy src

# Format code
uv run ruff format src tests

License

MIT License - see LICENSE for details.

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

office_wordcount-1.0.1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

office_wordcount-1.0.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file office_wordcount-1.0.1.tar.gz.

File metadata

  • Download URL: office_wordcount-1.0.1.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for office_wordcount-1.0.1.tar.gz
Algorithm Hash digest
SHA256 51c5783b8d0d3a08dff32b5ca9b789001a0e7107bfd2e4062f9cb3bfead6931b
MD5 a0deff56396e677da260856871521420
BLAKE2b-256 c3a3d8be46c2e783143e7bb6fa13644e0b50db007ab3ef597fd63c82282535b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for office_wordcount-1.0.1.tar.gz:

Publisher: publish.yml on bebe3/office-wordcount

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file office_wordcount-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for office_wordcount-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 68255c53f99ba7c40469ef06eb96b4e19c8646f208235e8716d52f24a12cae0a
MD5 0a61024e11885ec3c4250e646b3631a2
BLAKE2b-256 4a3958b91649878a83f244005de1872decf5e52bce6870e7a7014cf34e688563

See more details on using hashes here.

Provenance

The following attestation bundles were made for office_wordcount-1.0.1-py3-none-any.whl:

Publisher: publish.yml on bebe3/office-wordcount

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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