rafael gf
rafael gf

Reputation: 21

Reading and enconding(b64) pdf files in python

I'm trying to open a pdf file in binary and encode it in base64("utf-8"), but I'm getting this ERROR message:

AttributeError: '_io.BufferedReader' object has no attribute 'b64encode'

I'm working in windows 10 and python 3, code:

attach_file_name = os.getcwd() + '\doc.pdf'
attach_file = open(attach_file_name, "rb")
attach_file = attach_file.b64encode("utf-8")

Upvotes: 1

Views: 379

Answers (1)

PacketLoss
PacketLoss

Reputation: 5746

You need to use the base64 module.

import base64

with open(pod, 'rb') as pdf:
    encoded = base64.b64encode(pdf.read())

Upvotes: 1

Related Questions