diff --git a/html2text/__init__.py b/html2text/__init__.py
index 1f3e803fb7..61bd602973 100644
--- a/html2text/__init__.py
+++ b/html2text/__init__.py
@@ -14,6 +14,7 @@
from html2text import config
from html2text.utils import (
+ wide_unichr,
name2cp,
unifiable_n,
google_text_emphasis,
@@ -703,7 +704,7 @@ def charref(self, name):
return unifiable_n[c]
else:
try:
- return unichr(c)
+ return wide_unichr(c)
except NameError: # Python3
return chr(c)
@@ -720,7 +721,7 @@ def entityref(self, c):
return config.UNIFIABLE[c]
else:
try:
- return unichr(name2cp(c))
+ return wide_unichr(name2cp(c))
except NameError: # Python3
return chr(name2cp(c))
diff --git a/html2text/utils.py b/html2text/utils.py
index 0946930f4d..bc9fb48f45 100644
--- a/html2text/utils.py
+++ b/html2text/utils.py
@@ -1,8 +1,17 @@
import sys
from html2text import config
+import struct
from html2text.compat import htmlentitydefs
+# Based on http://stackoverflow.com/questions/7105874/valueerror-unichr-arg-not-in-range0x10000-narrow-python-build-please-hel
+def wide_unichr(i):
+ try:
+ return unichr(i)
+ except ValueError:
+ return struct.pack('i', i).decode('utf-32')
+
+
def name2cp(k):
if k == 'apos':