jcushman / pdfquery

A fast and friendly PDF scraping library.
MIT License
770 stars 89 forks source link

Can't concat str to bytes #68

Open gtdrakeley opened 5 years ago

gtdrakeley commented 5 years ago

When loading a pdf via pdf.load(), specifically the Pathfinder 2E playtest pdf, this error is returned:


  File "<stdin>", line 1, in <module>
  File "C:\Users\Graham Drakeley\.virtualenvs\Downloads-jPJuT-kW\lib\site-packages\pdfquery\pdfquery.py", line 385, in load
    self.tree = self.get_tree(*_flatten(page_numbers))
  File "C:\Users\Graham Drakeley\.virtualenvs\Downloads-jPJuT-kW\lib\site-packages\pdfquery\pdfquery.py", line 490, in get_tree
    page.set('page_label', self.doc.get_page_number(n))
  File "C:\Users\Graham Drakeley\.virtualenvs\Downloads-jPJuT-kW\lib\site-packages\pdfquery\pdfquery.py", line 271, in get_page_number
    page_label = label_format['P']+page_label
TypeError: can't concat str to bytes```
peturjokull commented 4 years ago

I had the same error so I added decode to the byte part and it worked for me

page_label = label_format['P'].decode('utf-8')+page_label

gskriptor commented 3 years ago

@peturjokull I tried your fix, but my script is just stuck running. When I stop it with ctl C I get this error message. My script is from the pdfquery examples. Seems like there is something up with the pdf.load() funciton.

^CTraceback (most recent call last): File "loadtest.py", line 4, in pdf.load() File "/usr/local/lib/python3.7/dist-packages/pdfquery/pdfquery.py", line 385, in load self.tree = self.get_tree(*_flatten(page_numbers)) File "/usr/local/lib/python3.7/dist-packages/pdfquery/pdfquery.py", line 487, in get_tree for n, page in pages: File "/usr/local/lib/python3.7/dist-packages/pdfquery/pdfquery.py", line 608, in return (self.get_layout(page) for page in self._cached_pages()) File "/usr/local/lib/python3.7/dist-packages/pdfquery/pdfquery.py", line 601, in get_layout self.interpreter.process_page(page) File "/usr/local/lib/python3.7/dist-packages/pdfminer/pdfinterp.py", line 842, in process_page self.device.end_page(page) File "/usr/local/lib/python3.7/dist-packages/pdfminer/converter.py", line 48, in end_page self.cur_item.analyze(self.laparams) File "/usr/local/lib/python3.7/dist-packages/pdfminer/layout.py", line 680, in analyze self.groups = self.group_textboxes(laparams, textboxes) File "/usr/local/lib/python3.7/dist-packages/pdfminer/layout.py", line 647, in group_textboxes if c == 0 and isany(obj1, obj2): File "/usr/local/lib/python3.7/dist-packages/pdfminer/layout.py", line 627, in isany objs = set(plane.find((x0, y0, x1, y1))) File "/usr/local/lib/python3.7/dist-packages/pdfminer/utils.py", line 322, in find for k in self._getrange(bbox): File "/usr/local/lib/python3.7/dist-packages/pdfminer/utils.py", line 285, in _getrange for x in drange(x0, x1, self.gridsize): KeyboardInterrupt

wennroy commented 8 months ago

I just encountered the same issue here. And I found the pdfquery.py script on current Github had successfully solved this issue, but the newest release package version v0.4.3 won't work. Try to compile the newest code and it should work.

To compile the newest code

Git clone the repo and then type, (If you would like to change a version just change it in setup.py)

pip install setuptools wheel
python setup.py sdist bdist_wheel

It will give you a .whl and .tar.gz file.

To pip install it

pip install pdfquery --find-links ./

replace ./ to the directory you put your compiled .whl.

To dig a little bit deeper

Codes in v0.4.3,

# handle string prefix
if 'P' in label_format:
    page_label = label_format['P']+page_label

Codes in current github,

# handle string prefix
if 'P' in label_format:
    page_label = smart_unicode_decode(label_format['P']) + page_label

They added a smart_unicode_decode to fix the byte could not add with str value.