Errors

UnicodeEncodeError and garbled output on Windows

Updated 2026-11-15·Upstream source ↗
Markdown view

The conversion itself succeeded; the damage happens afterwards, when Windows saves the text using a legacy code page. One flag avoids it, and we ran the same Word file through every way of saving that we could think of to show what each one does.

Short answer

Write the file with MarkItDown's own output flag instead of the shell's >:

Command
markitdown report.docx -o report.md

MarkItDown then writes UTF-8 itself and the shell never touches the text. In your own Python code, open the output file with encoding="utf-8" explicitly (below).

Tested with markitdown 0.1.9 · Python Windows Server 2025 runner (cp1252) with Command Prompt, PowerShell 7.6 and Windows PowerShell 5.1; MarkItDown 0.0.1a5 to 0.1.9; Linux lab for emulated code pages and the POSIX locale · 2026-11-15 · evidence: windows-redirect.json, windows-redirect-table.csv, windows-redirect-remedies.csv, windows-redirect-versions.csv

Ways to save the same file, and what each one did

One Word file containing café, 東京 and an emoji, converted with MarkItDown 0.1.9 on a Windows runner whose code page was Windows-1252, saved with each method in each shell:

Default settings: what happened to the three test strings
ShellHow it was savedFile is東京Emojicafé
cmd> outWindows-1252lostlostkept
cmd-oUTF-8keptkeptkept
pwsh 7> outWindows-1252lostlostkept
pwsh 7| Out-FileUTF-8lostlostlost
pwsh 7| Out-File -Encoding utf8UTF-8lostlostlost
pwsh 7| Set-ContentUTF-8lostlostlost
pwsh 7-oUTF-8keptkeptkept
pwsh 7> out after setting OutputEncoding to UTF-8Windows-1252lostlostkept
Windows PowerShell 5.1> outUTF-16lostlostlost
Windows PowerShell 5.1| Out-FileUTF-16lostlostlost
Windows PowerShell 5.1| Out-File -Encoding utf8UTF-8 + BOMlostlostlost
Windows PowerShell 5.1| Set-ContentUTF-8lostlostlost
Windows PowerShell 5.1-oUTF-8keptkeptkept
Windows PowerShell 5.1> out after setting OutputEncoding to UTF-8UTF-16lostlostlost
Default settings: what happened to the three test strings Source: Test T17 · run 43740808452 · markitdown 0.1.9 · windows-redirect-table.csv · Download CSV

Only -o kept everything in every shell. Every redirect or pipe lost the Japanese and the emoji, and Windows PowerShell 5.1 also wrote the file as UTF-16, so another program reading it as UTF-8 sees a space between every letter and a file twice the size. Nothing crashed: the exit code was 0 every time.

Which symptom do you have?

A crash: UnicodeEncodeError: 'gbk' / 'charmap' / 'cp932' codec can't encode character…

The codec name is the code page your Windows uses for text files: gbk on Simplified Chinese Windows, charmap (Windows-1252) on Western ones, cp932 on Japanese ones. We reproduced the three messages by telling Python to use each of those encodings (a real Chinese or Japanese Windows was not available to us), on the oldest releases, which still crashed:

markitdown 0.0.2 · Linux lab · stdout encoding set to gbk · 2026-11-15
$ markitdown report.docx > out.md
Traceback (most recent call last):
  File "/tmp/claude-0/v002/bin/markitdown", line 6, in <module>
    sys.exit(main())
             ~~~~^^
  File "/tmp/claude-0/v002/lib/python3.13/site-packages/markitdown/__main__.py", line 93, in main
    _handle_output(args, result)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/tmp/claude-0/v002/lib/python3.13/site-packages/markitdown/__main__.py", line 102, in _handle_output
    print(result.text_content)
    ~~~~~^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'gbk' codec can't encode character '\xef' in position 811: illegal multibyte sequence

raw log · Emulates Simplified Chinese Windows; not a real Chinese installation

markitdown 0.0.2 · Linux lab · stdout encoding set to cp1252 · 2026-11-15
$ markitdown report.docx > out.md
Traceback (most recent call last):
  File "/tmp/claude-0/v002/bin/markitdown", line 6, in <module>
    sys.exit(main())
             ~~~~^^
  File "/tmp/claude-0/v002/lib/python3.13/site-packages/markitdown/__main__.py", line 93, in main
    _handle_output(args, result)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/tmp/claude-0/v002/lib/python3.13/site-packages/markitdown/__main__.py", line 102, in _handle_output
    print(result.text_content)
    ~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/encodings/cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
           ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'charmap' codec can't encode characters in position 816-817: character maps to <undefined>

raw log · Emulates Western Windows

markitdown 0.0.2 · Linux lab · stdout encoding set to cp932 · 2026-11-15
$ markitdown report.docx > out.md
Traceback (most recent call last):
  File "/tmp/claude-0/v002/bin/markitdown", line 6, in <module>
    sys.exit(main())
             ~~~~^^
  File "/tmp/claude-0/v002/lib/python3.13/site-packages/markitdown/__main__.py", line 93, in main
    _handle_output(args, result)
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^
  File "/tmp/claude-0/v002/lib/python3.13/site-packages/markitdown/__main__.py", line 102, in _handle_output
    print(result.text_content)
    ~~~~~^^^^^^^^^^^^^^^^^^^^^
UnicodeEncodeError: 'cp932' codec can't encode character '\u2014' in position 740: illegal multibyte sequence

raw log · Emulates Japanese Windows

Release by release: what redirecting with > did (Windows runner, default settings)
MarkItDownWith >With -o
0.0.1a5crash (UnicodeEncodeError)kept
0.0.1crash (UnicodeEncodeError)kept
0.0.2crash (UnicodeEncodeError)kept
0.1.0characters replaced by ?kept
0.1.1characters replaced by ?kept
0.1.2characters replaced by ?kept
0.1.3characters replaced by ?kept
0.1.4characters replaced by ?kept
0.1.5characters replaced by ?kept
0.1.6characters replaced by ?kept
0.1.7characters replaced by ?kept
0.1.8characters replaced by ?kept
0.1.9characters replaced by ?kept
Release by release: what redirecting with > did (Windows runner, default settings) Source: Test T17 · 13 releases · windows-redirect-versions.csv · Download CSV

No crash, but ? where characters should be

Since 0.1.0 the command line replaces what it cannot encode instead of raising. Nothing tells you: your accents, bullets, Chinese, Japanese or emoji have silently become ?. This is the symptom most readers have now, and it is quieter than a crash, so check your output.

The file opens as gibberish, or is twice the expected size

Windows PowerShell 5.1 writes redirected text as UTF-16 (with a byte-order mark). The same text, saved with -o, is UTF-8. We did not capture screenshots of Notepad or VS Code, so we cannot show what each editor does with it.

Wrong characters that were never in the output: cafķ instead of café

That is the opposite problem: MarkItDown guessed the input file's encoding wrong, for example a Windows-1252 CSV. Tell it the charset with -c (see the CLI page) and read the CSV page.

Why Windows does this

MarkItDown produces Unicode text. To print it, Python must turn it into bytes. When the output goes to a console, Windows can display Unicode, but when it is redirected to a file or a pipe, Python uses the system's legacy code page rather than UTF-8: our runner reported cp1252 for redirected standard output in all three shells. -o skips the whole path: MarkItDown opens the file and writes UTF-8 itself.

If you really need > or a pipe

Some remedies work, but only in some shells. Each row below is a measured result for markitdown report.docx > out.md (or the pipe form in PowerShell):

Remedies for redirecting, per shell: which of the three test strings survived
RemedyCommand PromptPowerShell 7Windows PowerShell 5.1
Nothing (default settings)lost: Tokyo, emojilost: Tokyo, emojilost: Tokyo, emoji, cafe (file is UTF-16)
PYTHONUTF8=1all keptall keptall kept (file is UTF-16)
PYTHONIOENCODING=utf-8all keptall keptall kept (file is UTF-16)
Set the console OutputEncoding to UTF-8 firstnot applicablelost: Tokyo, emojilost: Tokyo, emoji, cafe (file is UTF-16)
PYTHONIOENCODING=cp437 (emulated)lost: Tokyo, emoji, cafelost: Tokyo, emoji, cafelost: Tokyo, emoji, cafe (file is UTF-16)
PYTHONIOENCODING=cp932 (emulated)lost: Tokyo, emoji, cafelost: Tokyo, emoji, cafelost: Tokyo, emoji, cafe (file is UTF-16)
PYTHONIOENCODING=cp936 (emulated)lost: Tokyo, emoji, cafelost: Tokyo, emoji, cafelost: Tokyo, emoji, cafe (file is UTF-16)
markitdown -o file.md (no redirect)all keptall keptall kept
Remedies for redirecting, per shell: which of the three test strings survived Source: Test T17 · the cp437, cp932 and cp936 rows are emulated · windows-redirect-remedies.csv · Download CSV
  • PYTHONUTF8=1 and PYTHONIOENCODING=utf-8 kept everything in Command Prompt and PowerShell 7. In Windows PowerShell 5.1 the characters survived too, but the file is still UTF-16. Set it for one command:
Command
set PYTHONUTF8=1
markitdown report.docx > report.md
  • Setting the console's output encoding did not help in either PowerShell version.
  • A Python told to use cp437, cp932 or cp936 lost the test strings, as expected; those rows are emulated and show what Python writes, not what a real Chinese or Japanese Windows would do.
  • chcp 65001 could not be tested, because a CI runner has no console; reports on GitHub say it does not help, and we cannot confirm or deny it.
  • Piping into another Python program: set the same variable on both sides, because the receiving program decodes with its own code page.

In your own Python code

Take the Markdown string from the result object and write it with an explicit encoding. Printing to a Windows console can fail or substitute for the same reason as the command line, and logging handlers need the same care.

Pythonapi-write-utf8.py
from pathlib import Path
from markitdown import MarkItDown

text = MarkItDown().convert("samples/report.docx").markdown
Path("report.md").write_text(text, encoding="utf-8")   # explicit encoding: the same bytes on every system

data = Path("report.md").read_bytes()
print("bytes written:", len(data))
print("starts with a byte-order mark:", data[:3] == b"\xef\xbb\xbf")
print("keeps café, 東京 and the emoji:", all(s in data.decode("utf-8") for s in ("café", "東京", "\U0001F642")))
print("line endings: CRLF" if b"\r\n" in data else "line endings: LF")
Output · markitdown 0.1.9 · Python 3.13.18 · 2026-11-15
bytes written: 850
starts with a byte-order mark: False
keeps café, 東京 and the emoji: True
line endings: LF

captured result

On Windows write_text translates line breaks to CRLF unless you pass newline="": see the next section.

One more Windows difference: line endings

Files written with -o on Windows contain CRLF line endings (36 in the 886-byte test file), while the same conversion on Linux contains LF; the content is otherwise identical. It matters for Git diffs and hashes, so normalise before comparing.

macOS, Linux, WSL and Docker

These are normally unaffected, because they use UTF-8. We tried to break Linux: with the C locale, Python 3.13 switches itself to UTF-8 and everything was kept. Only when we also switched that coercion off did a redirect lose the characters (7 ?), and -o still kept everything:

Version notes

  • since 0.1.0 The exception was replaced by a silent ?: releases up to 0.0.2 crashed, 0.1.0 to 0.1.9 replace (table above). -o kept everything in all 13 releases we ran.