I'm trying to extract some texts from PDF files using PyPDF2, but I'm encountering some problems. Here is my code and a pdf example file (with correct characters). You can download the pdf file here: https://github.com/giadabart/sharedfiles/blob/main/document-page259.pdf
def extractTextFromPdf(filename, ignore_newline=False):
from PyPDF2 import PdfFileReader as pfr
reader = pfr(filename)
content = '' # encoding = 'UTF8'
for p in reader.pages:
content += p.extractText()
if ignore_newline:
content = content.replace('\n', '')
return content
estrazione_prova = extractTextFromPdf('input/document-page259.pdf')
estrazione_prova
The output I get is:
'APPENDICEB.APPENDICECARTOGRAFICA EICONOGRAFICA\nTabella B.1\nSoggetto\nPianta degliortia OvestdelBattistero.\nAutore\nRanieriCorsi\nData\nNondatato\nTe c n i c a\n-\nDimensioni\n-\nIscrizioni\nPianta dellÕOrto attenentealla VenerabilOpera delDuomo diPisa\ndetto LÕOrto da SanGiovanni.\nCollo cazione\nPisa,Opera della PrimazialePisana,Archivio.\nDescrizione\nNeldisegno sono rappresentatigliorticoltivatiadOvestdelBat-\ntistero,la Casa dellÕOrtolano eilmuro divisorio checosteggiava\nla Porta ovestdelmonumento.Adovesttalepropriet‹ conÞnava\nconunÕaltra porzionediterreno dipropriet‹ della Dogana diPisa,\ncomeattesta lÕiscrizionelungo ilmarginesinistro deldisegno.\nNote\n-\nBibliograÞa\n-\n250\n'
What is wrong with the output is:
- the apostrophe which is read as "Õ";
- "à" becoming "‹";
- syllable "fi" turning into the greek letter "Þ";
- (sometimes) misplaced spaces between two or more consecutive words. Example: "Pianta degliortia OvestdelBattistero." should be "Pianta degli orti a Ovest del Battistero.
If I copy and paste the text from the pdf (document-page259.pdf) to a "LibreOffice Writer" file to understand if there is any problem with the original pdf, each character matches the original one. So, I guess there isn't any particular error in the pdf.
I also tried to copy and paste the text from the pdf (document-page259.pdf) to a "LibreOffice Calc" and it sets Unicode (UTF-16) as the character set. By doing so, I get the correct match between the original pdf text and the copied one.
Finally, I tried the following code to understand something about encoding, but it doesn't help to solve the problem.
estrazione_prova = extractTextFromPdf('input/document-page259.pdf')
x = estrazione_prova.encode()
print(x)
The output is:
b'APPENDICEB.APPENDICECARTOGRAFICA EICONOGRAFICA\nTabella B.1\nSoggetto\nPianta degliortia OvestdelBattistero.\nAutore\nRanieriCorsi\nData\nNondatato\nTe c n i c a\n-\nDimensioni\n-\nIscrizioni\nPianta dell\xc3\x95Orto attenentealla VenerabilOpera delDuomo diPisa\ndetto L\xc3\x95Orto da SanGiovanni.\nCollo cazione\nPisa,Opera della PrimazialePisana,Archivio.\nDescrizione\nNeldisegno sono rappresentatigliorticoltivatiadOvestdelBat-\ntistero,la Casa dell\xc3\x95Ortolano eilmuro divisorio checosteggiava\nla Porta ovestdelmonumento.Adovesttalepropriet\xe2\x80\xb9 con\xc3\x9enava\nconun\xc3\x95altra porzionediterreno dipropriet\xe2\x80\xb9 della Dogana diPisa,\ncomeattesta l\xc3\x95iscrizionelungo ilmarginesinistro deldisegno.\nNote\n-\nBibliogra\xc3\x9ea\n-\n250\n'
Please, could anyone give me an idea how to fix the problem?