Repository navigation
Closed
Description
Describe the bug
When calling unstructured.partition.doc.partition_doc with a doc file with multi-byte name (I checked: 文章.doc/风格.doc), it fails with an error.
To Reproduce
-
Create empty
*.docfile using Word, and name it文章.docor风格.doc. I attached that files below:
empty_docs.zip -
Run the code below:
from unstructured.partition.doc import partition_doc
doc_path = "文章.doc"
# doc_path = "风格.doc"
elements = partition_doc(filename=doc_path)- It will throw:
Traceback (most recent call last):
File "<home>\utf8error.py", line 4, in <module>
elements = partition_doc(filename=doc_path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<python>\site-packages\unstructured\documents\elements.py", line 605, in wrapper
elements = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "<python>\site-packages\unstructured\file_utils\filetype.py", line 731, in wrapper
elements = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "<python>\site-packages\unstructured\file_utils\filetype.py", line 687, in wrapper
elements = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "<python>\site-packages\unstructured\chunking\dispatch.py", line 74, in wrapper
elements = func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "<python>\site-packages\unstructured\partition\doc.py", line 89, in partition_doc
convert_office_doc(
File "<python>\site-packages\unstructured\partition\common.py", line 429, in convert_office_doc
message = output.stdout.decode().strip()
^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8a in position 32: invalid start byte
(I replaced private directory with <home>. Also replaced directory Python installed with <python>)
Expected behavior
The code should not throw an exception.
Screenshots
Environment Info
- Windows 11 Home
- Python 3.11.9
- unstructured: 0.15.13
Additional context
English(alphabet only) filename didn't cause an exception to be thrown.
Activity
scanny commentedon Sep 21, 2024
@Snowman-s I don't believe this is directly related to the filename. You can test that theory by making a copy of the file and changing its name to something like
document.doc. I believe you'll see the same results.You can see where this error occurs, the code is capturing the
sofficecommand output for logging purposes:https://github.com/Unstructured-IO/unstructured/blob/main/unstructured/partition/common/common.py#L309
Are you running on Windows? There are some possible problems with the encoding of the terminal output not being
utf-8.Snowman-s commentedon Sep 22, 2024
@scanny Thanks for reply!
I have re-tested the English filename and reconfirmed that it does not produce any errors.
During that verification, I noticed that it fails not only if the file name contains multi-byte characters, but also if the path to the file contains multi-byte characters.
Yes. I'm using Windows 11.
scanny commentedon Sep 22, 2024
Ahh, interesting. That leads me to believe that the input filename is echoed on
stdoutsomewhere and that's where it's failing (and why it's not failing until we try to read stdout). In any case, the encoding used on stdout on your machine is notutf-8it appears.Some useful detail on the underlying problem here:
python/cpython#105312
@Snowman-s what happens if you set
PYTHONENCODING=utf-8before running your code?https://stackoverflow.com/a/7865013/1902513
scanny commentedon Sep 22, 2024
Engineering note: one plausible solution to this is to avoid attempts to decode the captured stdout bytes and simply use
str(output.stdout)instead. Rationale:try/except UnicodeDecodeErrorblock.utf-8for the entire running Python process and subprocesses.Snowman-s commentedon Sep 23, 2024
@scanny
I ran
$env:PYTHONIOENCODING="utf-8:surrogateescape"; python <code>.py, and it still throws the same exception.Same for
sys.stdout.reconfigure(encoding='utf-8').I've forgotten about it until now, but here's the version of
soffice. (It is Windows 64 bit version.)scanny commentedon Sep 23, 2024
@Snowman-s that's good to know. That narrows down the possible solutions.
Engineering note: I think this rules out us being able to affect how LibreOffice encodes messages it writes to
stdout. The options I can think of are these:try/exceptblock as I mentioned above, falling back tostr(bytes_from_stdout)onUnicodeDecodeErrorwhich would be mostly readable.try/exceptand usechardetas a backup to auto-detect encoding.My vote is option 1 since this is for logging, not for UI.
huuufu commentedon Aug 31, 2026
Opened #4465 for this.
Two notes beyond the original report:
stderrdecode on the failure branch has the same problem, and it is reached exactly when a conversion fails — the case this issue is about — so it needed the same treatment. It also sits outside the surroundingtry.errors="backslashreplace"rather than option 1'sstr(bytes). It gets the same tolerance in one expression, and keeps the line pure ASCII: U+FFFD (whaterrors="replace"would give) is not encodable in cp932 or cp936, so alogging.FileHandleron an affected machine drops the whole record rather than logging it. Happy to switch tostr()if you would rather stay with the letter of option 1.Thanks @scanny — the analysis in this thread made the fix straightforward, and @Snowman-s for ruling out the
PYTHONIOENCODINGroute.fix: tolerate non-UTF-8 soffice output in doc/ppt conversion
fix: tolerate non-UTF-8 soffice output in doc/ppt conversion (Unstruc…