G
Guest
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
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