5

I am trying to send myself PDF files per E-mail with Python. I am able to send myself the binary code of a PDF file, but I am not able to reconstruct the PDF file from this binary code.

Here is how I obtain the binary code of a PDF file:

file = open('code.txt', 'w')
for line in open('somefile.pdf', 'rb').readlines():
    file.write(str(line))
file.close()

Here is how I try to create a PDF file from the binary code:

file = open('new.pdf', 'wb')
for line in open('code.txt', 'r').readlines():
    file.write(bytes(line))
file.close()

I then recieve this error:

Traceback (most recent call last): File "something.py", line 3, in file.write(bytes(line)) TypeError: string argument without an encoding

What did I do wrong?

12
  • 2
    This ... isn't a thing. Why are you doing this? Why call str on the lines in the first place, and why call bytes on them later? Why not just email the PDF as it is? Commented Mar 25, 2017 at 17:10
  • I don't know how. I need to be able to do it with Python. Commented Mar 25, 2017 at 17:11
  • You could do something like base64.b64encode(cPickle.dumps(open('somefile.pdf', 'rb').read())) then just undo that after Commented Mar 25, 2017 at 17:12
  • What happends if I encode it? Commented Mar 25, 2017 at 17:12
  • 1
    @Peter What's the point of using pickle? Commented Mar 25, 2017 at 17:30

2 Answers 2

5

In your first block, open file in binary write mode (wb), since you are writing binary to it. Also, you don't need to convert it explicitly to str. It should look like this:

file = open('code.txt', 'wb')
for line in open('somefile.pdf', 'rb').readlines():
    file.write(line)
file.close()

For second block, open file in read binary mode (rb). Here also, no need to explicitly convert to bytes. It should look like this:

file = open('new.pdf', 'wb')
for line in open('code.txt', 'rb').readlines():
    file.write(line)
file.close()

This should do the trick. But why do you need to convert it in the first place? Keeping file intact will save your hardwork and computational power.

Sign up to request clarification or add additional context in comments.

5 Comments

How is this different from shutil.copying the file? The file is not "converted".
I am trying to send myself a PDF file per e-mail. I don't know how to directly send the file or if that is even possible. This is why I am trying to convert the code of the PDF file into a string and also back again later.
@RonLauterbach You can attach and send pdf with python's email module. No need to convert it.
@Jay Bhavsar How do I do that?
@RonLauterbach this should help
3

Just to add. In my case, I was downloading the pdf file from an API and the 'response.content' came in base64 format. I also didn't need to write line by line I needed to convert the byte array first using:

import requests
import base64

response = requests.get(self.download_url,
                        allow_redirects=True,
                        headers=headers,
                        params=query_params)

bytes = base64.b64decode(response.content)

with open('file.pdf', 'wb') as f:
  f.write(bytes)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.