# UnicodeEncodeError and garbled output on Windows

> Garbled text, question marks or a UnicodeEncodeError when saving MarkItDown output on Windows? Use -o instead of >. We tested every way to save the same file.

Source: https://markitdown.dev/errors/unicodeencodeerror/ · Updated 2026-11-15·Upstream source ↗

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 `>`:
> 
> ```bash
> 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](#python)).

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](https://markitdown.dev/data/windows-redirect.json), [windows-redirect-table.csv](https://markitdown.dev/data/windows-redirect-table.csv), [windows-redirect-remedies.csv](https://markitdown.dev/data/windows-redirect-remedies.csv), [windows-redirect-versions.csv](https://markitdown.dev/data/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
| Shell | How it was saved | File is | 東京 | Emoji | café |
| --- | --- | --- | --- | --- | --- |
| cmd | `> out` | Windows-1252 | lost | lost | kept |
| cmd | `-o` | UTF-8 | kept | kept | kept |
| pwsh 7 | `> out` | Windows-1252 | lost | lost | kept |
| pwsh 7 | `| Out-File` | UTF-8 | lost | lost | lost |
| pwsh 7 | `| Out-File -Encoding utf8` | UTF-8 | lost | lost | lost |
| pwsh 7 | `| Set-Content` | UTF-8 | lost | lost | lost |
| pwsh 7 | `-o` | UTF-8 | kept | kept | kept |
| pwsh 7 | `> out after setting OutputEncoding to UTF-8` | Windows-1252 | lost | lost | kept |
| Windows PowerShell 5.1 | `> out` | UTF-16 | lost | lost | lost |
| Windows PowerShell 5.1 | `| Out-File` | UTF-16 | lost | lost | lost |
| Windows PowerShell 5.1 | `| Out-File -Encoding utf8` | UTF-8 + BOM | lost | lost | lost |
| Windows PowerShell 5.1 | `| Set-Content` | UTF-8 | lost | lost | lost |
| Windows PowerShell 5.1 | `-o` | UTF-8 | kept | kept | kept |
| Windows PowerShell 5.1 | `> out after setting OutputEncoding to UTF-8` | UTF-16 | lost | lost | lost |

*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:

```console
$ 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
```

*markitdown 0.0.2 · Linux lab · stdout encoding set to gbk · 2026-11-15*

```console
$ 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>
```

*markitdown 0.0.2 · Linux lab · stdout encoding set to cp1252 · 2026-11-15*

```console
$ 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
```

*markitdown 0.0.2 · Linux lab · stdout encoding set to cp932 · 2026-11-15*

Release by release: what redirecting with > did (Windows runner, default settings)
| MarkItDown | With > | With -o |
| --- | --- | --- |
| 0.0.1a5 | crash (UnicodeEncodeError) | kept |
| 0.0.1 | crash (UnicodeEncodeError) | kept |
| 0.0.2 | crash (UnicodeEncodeError) | kept |
| 0.1.0 | characters replaced by ? | kept |
| 0.1.1 | characters replaced by ? | kept |
| 0.1.2 | characters replaced by ? | kept |
| 0.1.3 | characters replaced by ? | kept |
| 0.1.4 | characters replaced by ? | kept |
| 0.1.5 | characters replaced by ? | kept |
| 0.1.6 | characters replaced by ? | kept |
| 0.1.7 | characters replaced by ? | kept |
| 0.1.8 | characters replaced by ? | kept |
| 0.1.9 | characters 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](https://markitdown.dev/cli/)) and read [the CSV page](https://markitdown.dev/formats/csv-json-xml/).

## 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
| Remedy | Command Prompt | PowerShell 7 | Windows PowerShell 5.1 |
| --- | --- | --- | --- |
| `Nothing (default settings)` | lost: Tokyo, emoji | lost: Tokyo, emoji | lost: Tokyo, emoji, cafe (file is UTF-16) |
| `PYTHONUTF8=1` | all kept | all kept | all kept (file is UTF-16) |
| `PYTHONIOENCODING=utf-8` | all kept | all kept | all kept (file is UTF-16) |
| `Set the console OutputEncoding to UTF-8 first` | not applicable | lost: Tokyo, emoji | lost: Tokyo, emoji, cafe (file is UTF-16) |
| `PYTHONIOENCODING=cp437 (emulated)` | lost: Tokyo, emoji, cafe | lost: Tokyo, emoji, cafe | lost: Tokyo, emoji, cafe (file is UTF-16) |
| `PYTHONIOENCODING=cp932 (emulated)` | lost: Tokyo, emoji, cafe | lost: Tokyo, emoji, cafe | lost: Tokyo, emoji, cafe (file is UTF-16) |
| `PYTHONIOENCODING=cp936 (emulated)` | lost: Tokyo, emoji, cafe | lost: Tokyo, emoji, cafe | lost: Tokyo, emoji, cafe (file is UTF-16) |
| `markitdown -o file.md (no redirect)` | all kept | all kept | all 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 Prompt**

```bash
set PYTHONUTF8=1
markitdown report.docx > report.md
```

**PowerShell 7**

```bash
$env: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.

```python
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](https://markitdown.dev/evidence/snippets/api-write-utf8/result.json)

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:

> **Evidence:** Linux lab, markitdown 0.1.9, LC_ALL=C with and without PYTHONCOERCECLOCALE=0 PYTHONUTF8=0: log evidence/T39/posix-locale.log. We did not test a minimal Docker image.

## 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.

## Related

- [Install on Windows 11](https://markitdown.dev/install/windows/): The full Windows install walkthrough.
- [CLI reference](https://markitdown.dev/cli/): The -o and -c flags in the CLI reference.
- [CSV, JSON, XML, text](https://markitdown.dev/formats/csv-json-xml/): Input-side charset detection for CSV and text files.
- [Error reference](https://markitdown.dev/errors/): Every other MarkItDown error.
- [Python API guide](https://markitdown.dev/python-api/): Writing results from Python code.

---
Independent guide to the open-source MarkItDown library. Not affiliated with or endorsed by Microsoft. Official source: https://github.com/microsoft/markitdown
