Converting PDF to text: “Text extraction is not allowed”












1















I am trying to convert PDF to text in Python. But it is giving me an error:




PDFTextExtractionNotAllowed: Text extraction is not allowed: <_io.BufferedReader name='C:UsersDownloadsFacts_for_2017.pdf'>




Code which I am using is:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
retstr = io.StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)
data = retstr.getvalue()

return data


if __name__ == '__main__':
text = pdfparser(Input_path)


Can anyone help me?



File path is:



https://drive.google.com/file/d/1RyR-J-EwMywL6BqsYbl4Ocm96VzCYrM7/view?usp=sharing










share|improve this question

























  • Be carefull, you are not closing your fp

    – BlueSheepToken
    Jan 2 at 16:37











  • Thanks, but it is still not working.

    – Nishant Tripathi
    Jan 2 at 16:41











  • Possibly same issue as this issue, where PDF is marked to not allow extraction: stackoverflow.com/questions/39981980/…

    – bdk
    Jan 2 at 18:01


















1















I am trying to convert PDF to text in Python. But it is giving me an error:




PDFTextExtractionNotAllowed: Text extraction is not allowed: <_io.BufferedReader name='C:UsersDownloadsFacts_for_2017.pdf'>




Code which I am using is:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
retstr = io.StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)
data = retstr.getvalue()

return data


if __name__ == '__main__':
text = pdfparser(Input_path)


Can anyone help me?



File path is:



https://drive.google.com/file/d/1RyR-J-EwMywL6BqsYbl4Ocm96VzCYrM7/view?usp=sharing










share|improve this question

























  • Be carefull, you are not closing your fp

    – BlueSheepToken
    Jan 2 at 16:37











  • Thanks, but it is still not working.

    – Nishant Tripathi
    Jan 2 at 16:41











  • Possibly same issue as this issue, where PDF is marked to not allow extraction: stackoverflow.com/questions/39981980/…

    – bdk
    Jan 2 at 18:01
















1












1








1








I am trying to convert PDF to text in Python. But it is giving me an error:




PDFTextExtractionNotAllowed: Text extraction is not allowed: <_io.BufferedReader name='C:UsersDownloadsFacts_for_2017.pdf'>




Code which I am using is:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
retstr = io.StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)
data = retstr.getvalue()

return data


if __name__ == '__main__':
text = pdfparser(Input_path)


Can anyone help me?



File path is:



https://drive.google.com/file/d/1RyR-J-EwMywL6BqsYbl4Ocm96VzCYrM7/view?usp=sharing










share|improve this question
















I am trying to convert PDF to text in Python. But it is giving me an error:




PDFTextExtractionNotAllowed: Text extraction is not allowed: <_io.BufferedReader name='C:UsersDownloadsFacts_for_2017.pdf'>




Code which I am using is:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
retstr = io.StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)
data = retstr.getvalue()

return data


if __name__ == '__main__':
text = pdfparser(Input_path)


Can anyone help me?



File path is:



https://drive.google.com/file/d/1RyR-J-EwMywL6BqsYbl4Ocm96VzCYrM7/view?usp=sharing







python python-3.x pdfminer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 17:31









Craicerjack

3,78422030




3,78422030










asked Jan 2 at 16:31









Nishant TripathiNishant Tripathi

61




61













  • Be carefull, you are not closing your fp

    – BlueSheepToken
    Jan 2 at 16:37











  • Thanks, but it is still not working.

    – Nishant Tripathi
    Jan 2 at 16:41











  • Possibly same issue as this issue, where PDF is marked to not allow extraction: stackoverflow.com/questions/39981980/…

    – bdk
    Jan 2 at 18:01





















  • Be carefull, you are not closing your fp

    – BlueSheepToken
    Jan 2 at 16:37











  • Thanks, but it is still not working.

    – Nishant Tripathi
    Jan 2 at 16:41











  • Possibly same issue as this issue, where PDF is marked to not allow extraction: stackoverflow.com/questions/39981980/…

    – bdk
    Jan 2 at 18:01



















Be carefull, you are not closing your fp

– BlueSheepToken
Jan 2 at 16:37





Be carefull, you are not closing your fp

– BlueSheepToken
Jan 2 at 16:37













Thanks, but it is still not working.

– Nishant Tripathi
Jan 2 at 16:41





Thanks, but it is still not working.

– Nishant Tripathi
Jan 2 at 16:41













Possibly same issue as this issue, where PDF is marked to not allow extraction: stackoverflow.com/questions/39981980/…

– bdk
Jan 2 at 18:01







Possibly same issue as this issue, where PDF is marked to not allow extraction: stackoverflow.com/questions/39981980/…

– bdk
Jan 2 at 18:01














1 Answer
1






active

oldest

votes


















0














You are getting the error because, the indentation is wrong in the line with data = retstr.getvalue(), it should be outside of the for loop.



However, after fixing that, I came across some other issues , so I am providing the full code below:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
# retstr = io.StringIO() #This will cause -- `TypeError: unicode argument expected, got 'str'`
retstr = io.BytesIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)

data = retstr.getvalue() #Indentation was worng here
fp.close()
#print(data)
return data


if __name__ == '__main__':
#PDF file you provied is encrypted with blank password, we need to decrypt it
path = sys.argv[1]
from subprocess import call
import os
pdf_filename = os.path.basename(path)
file_name, extension = os.path.splitext(pdf_filename)
pdf_filename_decr = str(file_name) + "_decr" + extension
call('qpdf --password=%s --decrypt %s %s' %('', path, pdf_filename_decr), shell=True)

text = pdfparser(pdf_filename_decr)





share|improve this answer


























  • This indentation was introduced by someone who has edited the code. There was not any indentation before !

    – BlueSheepToken
    Jan 2 at 18:55











  • Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

    – Nishant Tripathi
    Jan 2 at 19:08











  • It will save in the current working directory.

    – SDX
    Jan 2 at 19:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009871%2fconverting-pdf-to-text-text-extraction-is-not-allowed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are getting the error because, the indentation is wrong in the line with data = retstr.getvalue(), it should be outside of the for loop.



However, after fixing that, I came across some other issues , so I am providing the full code below:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
# retstr = io.StringIO() #This will cause -- `TypeError: unicode argument expected, got 'str'`
retstr = io.BytesIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)

data = retstr.getvalue() #Indentation was worng here
fp.close()
#print(data)
return data


if __name__ == '__main__':
#PDF file you provied is encrypted with blank password, we need to decrypt it
path = sys.argv[1]
from subprocess import call
import os
pdf_filename = os.path.basename(path)
file_name, extension = os.path.splitext(pdf_filename)
pdf_filename_decr = str(file_name) + "_decr" + extension
call('qpdf --password=%s --decrypt %s %s' %('', path, pdf_filename_decr), shell=True)

text = pdfparser(pdf_filename_decr)





share|improve this answer


























  • This indentation was introduced by someone who has edited the code. There was not any indentation before !

    – BlueSheepToken
    Jan 2 at 18:55











  • Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

    – Nishant Tripathi
    Jan 2 at 19:08











  • It will save in the current working directory.

    – SDX
    Jan 2 at 19:14
















0














You are getting the error because, the indentation is wrong in the line with data = retstr.getvalue(), it should be outside of the for loop.



However, after fixing that, I came across some other issues , so I am providing the full code below:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
# retstr = io.StringIO() #This will cause -- `TypeError: unicode argument expected, got 'str'`
retstr = io.BytesIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)

data = retstr.getvalue() #Indentation was worng here
fp.close()
#print(data)
return data


if __name__ == '__main__':
#PDF file you provied is encrypted with blank password, we need to decrypt it
path = sys.argv[1]
from subprocess import call
import os
pdf_filename = os.path.basename(path)
file_name, extension = os.path.splitext(pdf_filename)
pdf_filename_decr = str(file_name) + "_decr" + extension
call('qpdf --password=%s --decrypt %s %s' %('', path, pdf_filename_decr), shell=True)

text = pdfparser(pdf_filename_decr)





share|improve this answer


























  • This indentation was introduced by someone who has edited the code. There was not any indentation before !

    – BlueSheepToken
    Jan 2 at 18:55











  • Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

    – Nishant Tripathi
    Jan 2 at 19:08











  • It will save in the current working directory.

    – SDX
    Jan 2 at 19:14














0












0








0







You are getting the error because, the indentation is wrong in the line with data = retstr.getvalue(), it should be outside of the for loop.



However, after fixing that, I came across some other issues , so I am providing the full code below:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
# retstr = io.StringIO() #This will cause -- `TypeError: unicode argument expected, got 'str'`
retstr = io.BytesIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)

data = retstr.getvalue() #Indentation was worng here
fp.close()
#print(data)
return data


if __name__ == '__main__':
#PDF file you provied is encrypted with blank password, we need to decrypt it
path = sys.argv[1]
from subprocess import call
import os
pdf_filename = os.path.basename(path)
file_name, extension = os.path.splitext(pdf_filename)
pdf_filename_decr = str(file_name) + "_decr" + extension
call('qpdf --password=%s --decrypt %s %s' %('', path, pdf_filename_decr), shell=True)

text = pdfparser(pdf_filename_decr)





share|improve this answer















You are getting the error because, the indentation is wrong in the line with data = retstr.getvalue(), it should be outside of the for loop.



However, after fixing that, I came across some other issues , so I am providing the full code below:



import sys
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter
from pdfminer.layout import LAParams
import io


def pdfparser(data):
fp = open(data, 'rb')
rsrcmgr = PDFResourceManager()
# retstr = io.StringIO() #This will cause -- `TypeError: unicode argument expected, got 'str'`
retstr = io.BytesIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
interpreter = PDFPageInterpreter(rsrcmgr, device)

for page in PDFPage.get_pages(fp):
interpreter.process_page(page)

data = retstr.getvalue() #Indentation was worng here
fp.close()
#print(data)
return data


if __name__ == '__main__':
#PDF file you provied is encrypted with blank password, we need to decrypt it
path = sys.argv[1]
from subprocess import call
import os
pdf_filename = os.path.basename(path)
file_name, extension = os.path.splitext(pdf_filename)
pdf_filename_decr = str(file_name) + "_decr" + extension
call('qpdf --password=%s --decrypt %s %s' %('', path, pdf_filename_decr), shell=True)

text = pdfparser(pdf_filename_decr)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 18:30

























answered Jan 2 at 18:18









SDXSDX

113




113













  • This indentation was introduced by someone who has edited the code. There was not any indentation before !

    – BlueSheepToken
    Jan 2 at 18:55











  • Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

    – Nishant Tripathi
    Jan 2 at 19:08











  • It will save in the current working directory.

    – SDX
    Jan 2 at 19:14



















  • This indentation was introduced by someone who has edited the code. There was not any indentation before !

    – BlueSheepToken
    Jan 2 at 18:55











  • Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

    – Nishant Tripathi
    Jan 2 at 19:08











  • It will save in the current working directory.

    – SDX
    Jan 2 at 19:14

















This indentation was introduced by someone who has edited the code. There was not any indentation before !

– BlueSheepToken
Jan 2 at 18:55





This indentation was introduced by someone who has edited the code. There was not any indentation before !

– BlueSheepToken
Jan 2 at 18:55













Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

– Nishant Tripathi
Jan 2 at 19:08





Thanks for the response. I want to ask where pdf_filename_decr will save? When I run this program, it gives FileNotFound error.

– Nishant Tripathi
Jan 2 at 19:08













It will save in the current working directory.

– SDX
Jan 2 at 19:14





It will save in the current working directory.

– SDX
Jan 2 at 19:14




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009871%2fconverting-pdf-to-text-text-extraction-is-not-allowed%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory