Converting PDF to text: “Text extraction is not allowed”
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
add a comment |
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
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
add a comment |
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
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
python python-3.x pdfminer
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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