Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:42:10+00:00 2026-05-17T15:42:10+00:00

Guys i had posted a question earlier pypdf python tool .dont mark this as

  • 0

Guys i had posted a question earlier pypdf python tool .dont mark this as duplicate as i get this error indicated below

  import sys
  import pyPdf

  def convertPdf2String(path):
      content = ""
      # load PDF file
      pdf = pyPdf.PdfFileReader(file(path, "rb"))
      # iterate pages
      for i in range(0, pdf.getNumPages()):
          # extract the text from each page
          content += pdf.getPage(i).extractText() + " \n"
      # collapse whitespaces
      content = u" ".join(content.replace(u"\xa0", u" ").strip().split())
      return content

  # convert contents of a PDF file and store retult to TXT file
  f = open('a.txt','w+')
  f.write(convertPdf2String(sys.argv[1]))
  f.close()

  # or print contents to the standard out stream
  print convertPdf2String("/home/tom/Desktop/Hindi_Book.pdf").encode("ascii", "xmlcharrefreplace")

I get this error for a the 1st pdf file
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
and the following error for this pdf http://www.envis-icpe.com/pointcounterpointbook/Hindi_Book.pdf

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 38: ordinal not in range(128)

How to resolve this

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:42:11+00:00Added an answer on May 17, 2026 at 3:42 pm

    I tried it myself and got the same result. Ignore my comment, I hadn’t seen that you’re writing the output to a file as well. This is the problem:

    f.write(convertPdf2String(sys.argv[1]))
    

    As convertPdf2String returns a Unicode string, but file.write can only write bytes, the call to f.write tries to automatically convert the Unicode string using ASCII encoding. As the PDF obviously contains non-ASCII characters, that fails. So it should be something like

    f.write(convertPdf2String(sys.argv[1]).encode("utf-8"))
    # or
    f.write(convertPdf2String(sys.argv[1]).encode("ascii", "xmlcharrefreplace"))
    

    EDIT:

    The working source code, only one line changed.

    # Execute with "Hindi_Book.pdf" in the same directory
    import sys
    import pyPdf
    
    def convertPdf2String(path):
        content = ""
        # load PDF file
        pdf = pyPdf.PdfFileReader(file(path, "rb"))
        # iterate pages
        for i in range(0, pdf.getNumPages()):
            # extract the text from each page
            content += pdf.getPage(i).extractText() + " \n"
        # collapse whitespaces
        content = u" ".join(content.replace(u"\xa0", u" ").strip().split())
        return content
    
    # convert contents of a PDF file and store retult to TXT file
    f = open('a.txt','w+')
    f.write(convertPdf2String(sys.argv[1]).encode("ascii", "xmlcharrefreplace"))
    f.close()
    
    # or print contents to the standard out stream
    print convertPdf2String("Hindi_Book.pdf").encode("ascii", "xmlcharrefreplace")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had previously also asked a question on this and was unable to get
Hey guys i had an idea yesterday. Can you help me with this. Here
Sorry guys, not being lazy, I know others have had the same error message
Ok guys, So I was watching inception yesterday and I had this idea. I
Hai guys, I had this doubt for a long time now. Now being a
------------------NOTICE---------------- Sorry guys, made a stupid mistake, I had found this fix already: https://devforums.apple.com/message/32282
Guys, I've came across this problem I can't resolve myselg, I'm pretty sure I
I posted this over at reddit but it didn't gain any momentum. So here
I wanted to share with you guys a function I had created to see
Hey guys I wanted to know if I get get some advice from some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.