Python Forum
builtins.TypeError: a bytes-like object is required, not 'str'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
builtins.TypeError: a bytes-like object is required, not 'str'
#1
I am just getting started with python. Apologies if this is awkwardly presented

The syntax below worked in the python 2 embedded in SPSS. I am trying to use it in python 3. I did have to change the print commands to have parentheses.

The goal is to take a set of filled in fillable PDF forms and extract the filled in information and put it into a pipe-separated text file with one line per input form.
--- this is where the problem occurs. my vaalue is the contents of the /V element. This supposed to remove "\r" and replace it with "~".
if "\\r" in myvalue:
myvalue=re.sub("\r", "~",myvalue)
---- notes
The elements in the {} have a flag paired with some text. e.g., /T for the field name, /TU for the tool tip for when hover inside a form, and most importantly
/V for the filled in information.

As is seen in the python window below
/T prints okay
/V prints okay except when there are more than one line in the information. \r signals a newline. However, the Pipe-Separated does not work when read into packages so needs to be replaced. FirstName, LastName are okay. SayFewLines has some \r so gets the problem.

I do not see how to attach example inputs, but I would be glad to send the empty PDF form and 4 filled in cases with made up data. I can also sent the target.txt that is created via python2 in SPSS.



----- This is what was in the python window
evaluating 51 lines of code...
FirstName |
MiddEleInitial |
LastName |
GPA |
SayFewLines |
CheckedCheckBox |
UncheckedCheckBox |
State |
Handedness |
ListBoxPickALL |
ListBoxPick1 |
single |
package |
NotToolTip |
var names line completed
FirstName {'/FT': '/Tx', '/T': 'FirstName', '/TU': 'Given or Chistian name', '/Ff': 20971520, '/V': 'Alfred'}
Alfred
MiddleInitial {'/FT': '/Tx', '/T': 'MiddleInitial', '/TU': '', '/V': 'A.'}
A.
LastName {'/FT': '/Tx', '/T': 'LastName', '/TU': 'Surname or Family name', '/V': 'Alpha'}
Alpha
GPA {'/FT': '/Tx', '/T': 'GPA', '/TU': 'Grade Point Average', '/Ff': 4194304, '/V': '1.00'}
1.00
SayFewLines {'/FT': '/Tx', '/T': 'SayFewLines', '/TU': 'Put in a few lines of arbitrary text', '/Ff': 4096, '/V': b"If I had a hammer, I'd hammer in the morning\rI'd hammer in the evening All over this land\rI'd hammer out danger, I'd hammer out warning\rI'd hammer out the love between My brothers and my sisters\rAll over this land."}
Traceback (most recent call last):
Python Shell, prompt 16, line 45
builtins.TypeError: a bytes-like object is required, not 'str'


------ This is the python code
# extract filled in data from fillable PDF files
# put it into a pipe-separated TXT file
# use print commands while shaking down this job
# comment them out for large run
import sys
sys.path.append("c:/python37/lib/site-packages")
import os
import re
import glob
import PyPDF2

path = 'G:/'
outfile = open("G:/target.txt","w")

# get field names to put in first line of TXT file as variable names
firstpdf = "G:/all controls case 1.pdf"
open(firstpdf, "rb")
reader = PyPDF2.PdfFileReader(firstpdf)
page = reader.getPage(0)
text = page.extractText()
reader.numPages
for k, v in reader.getFields().items():
    myvalue = v.get('/T')
    print (myvalue, "|")
    outfile.write(myvalue)
    outfile.write("|")
outfile.write("\n")

print ("var names line completed")

# get filled in data
for file in glob.glob(path + "*.pdf"):
    if file.endswith(".pdf"):
        mycase= os.path.join(path, file)
        pdf = open(mycase, "rb")
        reader = PyPDF2.PdfFileReader(pdf)
        page = reader.getPage(0)
        text = page.extractText()
        reader.numPages
        for k, v in reader.getFields().items():
            print (k, v)
            myvalue = v.get('/V',"blank")
            if isinstance(myvalue,(list,)): 
                myvalue = ','.join(myvalue)
            if "\\r" in myvalue:
                myvalue=re.sub("\\r", "~",myvalue)
            print (myvalue)
            outfile.write(myvalue)
            outfile.write("|")
    outfile.write("\n")
outfile.close()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 269 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 444 Dec-30-2023, 04:35 PM
Last Post: deanhystad
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  TypeError: 'NoneType' object is not callable akbarza 4 920 Aug-24-2023, 05:14 PM
Last Post: snippsat
  [NEW CODER] TypeError: Object is not callable iwantyoursec 5 1,261 Aug-23-2023, 06:21 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable #1 isdito2001 1 1,045 Jan-21-2023, 12:43 AM
Last Post: Yoriz
  TypeError: a bytes-like object is required ZeroX 13 3,838 Jan-07-2023, 07:02 PM
Last Post: deanhystad
  TypeError: 'float' object is not callable TimofeyKolpakov 3 1,372 Dec-04-2022, 04:58 PM
Last Post: TimofeyKolpakov
  API Post issue "TypeError: 'str' object is not callable" makeeley 2 1,833 Oct-30-2022, 12:53 PM
Last Post: makeeley
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,166 Sep-19-2022, 02:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020