Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49124

io module and pdf question

Date 2013-06-25 04:18 +0000
From <jyoung79@kc.rr.com>
Subject io module and pdf question
Newsgroups comp.lang.python
Message-ID <mailman.3795.1372133932.3114.python-list@python.org> (permalink)

Show all headers | View raw


Would like to get your opinion on this.  Currently to get the metadata out of a pdf file, I loop through the guts of the file.  I know it's not the greatest idea to do this, but I'm trying to avoid extra modules, etc.

Adobe javascript was used to insert the metadata, so the added data looks something like this:

XYZ:colorList="DarkBlue,Yellow"

With python 2.7, it successfully loops through the file contents and I'm able to find the line that contains "XYZ:colorList".

However, when I try to run it with python 3, it errors:

  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/codecs.py", line 300, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte

I've done some research on this, and it looks like encoding it to latin-1 works.  I also found that if I use the io module, it will work on both python 2.7 and 3.3.  For example:

--------------
import io
import os

pdfPath = '~/Desktop/test.pdf'

colorlistData = ''

with io.open(os.path.expanduser(pdfPath), 'r', encoding='latin-1') as f:
    for i in f:
        if 'XYZ:colorList' in i:
            colorlistData = i.split('XYZ:colorList')[1]
            break

print(colorlistData)
--------------

As you can tell, I'm clueless in how exactly this works and am hoping someone can give me some insight on:
1. Is there another way to get metadata out of a pdf without having to install another module?
2. Is it safe to assume pdf files should always be encoded as latin-1 (when trying to read it this way)?  Is there a chance they could be something else?
3. Is the io module a good way to pursue this?

Thanks for your help!

Jay

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

io module and pdf question <jyoung79@kc.rr.com> - 2013-06-25 04:18 +0000
  Re: io module and pdf question rusi <rustompmody@gmail.com> - 2013-06-24 23:33 -0700
    Re: io module and pdf question Christian Gollwitzer <auriocus@gmx.de> - 2013-06-25 09:18 +0200
  Re: io module and pdf question wxjmfauth@gmail.com - 2013-06-26 07:11 -0700

csiph-web