UD3-Lab / mintEMU

mintEMU - The Legacy of the European Postgraduate Master in Urbanism at TU Delft: A Text Mining Approach
Other
1 stars 0 forks source link

choose/adapt encoding when converting pdf in Python #18

Closed alwil closed 1 year ago

alwil commented 1 year ago

Tried the following:

def find_codec(file): with fitz.open(file) as doc: page = doc[1] for i in all_codecs: for j in all_codecs: try: text = page.get_text().encode(i).decode(j) print('conversion from ', i, ' to ', j ,'successful', text[:10]) except: pass


- check the encoding with the `chardet` package : 
   + **RESULT**: No encoding detected

import chardet

with open(file,'rb') as f: data = "" data = f.read() encoding=chardet.detect(data)['encoding']

encoding


- check the encoding with the `charset_normalizer` package : 
   + **RESULT**: No encoding detected

from charset_normalizer import from_path

results = from_path('./my_subtitle.srt')

print(str(results.best()))


- checked the problematic file encoding properties in Adobe Acrobat Reader: `File->Properties-> Fonts-> Encoding` ( alternative in Python: `page.get_fonts()`)
   + **RESULT**: the encoding seems to be "Identy-H"

- I had a short search of the `Identity-H` term: [here](https://community.adobe.com/t5/acrobat-discussions/identity-h-encoding/td-p/10400841) and  [here](https://tex.stackexchange.com/questions/526157/what-is-identity-h-encoding-should-it-be-avoided-and-if-so-how)
   + **RESULT**: It seems that it's a common encoding, however the they need a “ToUnicode CMap”. This is normally provided unless non-Unicode fonts are used in InDesign or a similar software: 
    > Exporting from InDesign or using Acrobat PDFMaker for Word should get this right, unless non-Unicode fonts are used. Don’t use such fonts.

- I tested if there's “ToUnicode CMap” by copy-pasting text from PDF to various text editors. It's not possible to find out directly whether file has the “ToUnicode CMap”, but if it does, a simple copy-paste should work fine:
> To extract text when this encoding is used, the PDF also needs a “ToUnicode CMap”. You cannot see if one of these is present.

> An easy test to see if a ToUnicode CMap is present for a Identity-H font: If copy&paste mostly works, you certainly have a ToUnicode CMap
   + **RESULT**: It seems that the mapping is missing. 

There is some discussion on how to deal with this issue on the PyMUPDF GitHub page:
- https://github.com/pymupdf/PyMuPDF/issues/365
- https://github.com/pymupdf/PyMuPDF/issues/530

But it seems focusing on the issue would drive us away from the main objectives of the project. 
alwil commented 1 year ago

@cforgaci , Please see my comment explaining what steps were made to work on the encoding issue and possible root of the problem. I suggest not focusing on this issue at the moment and come back to it if there's more PDF files with similar problem.

If you agree, please feel free to mark the issue as closed.

cforgaci commented 1 year ago

Thank you @alwil for following up on this. I agree we put this on hold and focus our attention on the main objectives of the project. I am closing this issue, as you suggest.