I am working on invoice parsing using invoice2data library. This library has predefined templates in YAML for parsing invoices. But when I am running the samples it is giving me YAML parsing error for all the templates

Runnung it as :

invoice2data --input-reader tesseract FlipkartInvoice.pdf

Exception:

Traceback (most recent call last):
File "/home/webwerks/.local/bin/invoice2data", line 10, in <module>
sys.exit(main())
File "/home/webwerks/.local/lib/python3.5/site-packages/invoice2data/main.py", line 191, in main
templates += read_templates()
File "/home/webwerks/.local/lib/python3.5/site-packages/invoice2data/extract/loader.py", line 88, in read_templates
tpl = ordered_load(template_file.read())
File "/home/webwerks/.local/lib/python3.5/site-packages/invoice2data/extract/loader.py", line 36, in ordered_load
return yaml.load(stream, OrderedLoader)
File "/usr/local/lib/python3.5/dist-packages/yaml/__init__.py", line 112, in load
loader = Loader(stream)
File "/usr/local/lib/python3.5/dist-packages/yaml/loader.py", line 44, in __init__
Reader.__init__(self, stream)
File "/usr/local/lib/python3.5/dist-packages/yaml/reader.py", line 74, in __init__
self.check_printable(stream)
File "/usr/local/lib/python3.5/dist-packages/yaml/reader.py", line 144, in check_printable
'unicode', "special characters are not allowed")
yaml.reader.ReaderError: unacceptable character #x0082: special characters are not allowed
in "<unicode string>", position 312

The last line says:

File "/usr/local/lib/python3.5/dist-packages/yaml/reader.py", line 144, in check_printable
'unicode', "special characters are not allowed")
yaml.reader.ReaderError: unacceptable character #x0082: special characters are not allowed
in "<unicode string>", position 312

I have checked the templates. All are valid in the UTF-8 format.
The issue seems to be with the python-yaml package.Anyone has encountered this issue?

1 Answers

3
Anthon On

That your input is valid UTF-8 is irrelevant, as YAML source should only accept a subset of Unicode code points (independent of UTF-8 or some other encoding).

In particular it does only support the printable subset of Unicode and the old YAML 1.1 specification, the one that PyYAML supports, elaborates that with:

The allowed character range explicitly excludes the surrogate block #xD800-#xDFFF, DEL #x7F, the C0 control block #x0-#x1F (except for #x9, #xA, and #xD), the C1 control block #x80-#x9F, #xFFFE, and #xFFFF. Any such characters must be presented using escape sequences.

So the non-printable "BREAK PERMITTED HERE" codepoint, 0x0082 is clearly disallowed (and is not one of those things PyYAML should allow, but doesn't).