0

I'm just trying to read mail attachment (PDF file attached) in memory so that i can use that it for pyPdf2

Here is the basic code,

from pyPdf2 import PdfFileReader
.
.
for attachment in message.attachments:
   attachment_name = attachment[0].decode()
   attachment_content = attachment[1].decode()

Here i just wanted to know that how much pages (page count) attached pdf has using attachment_content & attachment_name variable,

For more info, There is a method in pyPdf2, using that we can get pages count.

pdf_instance = PdfFileReader(file(file_path, "rb")) #But i'm stuck here, how to specify here *file_path* & not able to use that *attachment_content* & *attachment_name* varible
pdf_instance.getNumPages()

Any body can help me out here to use PdfFileReader

Thanks.

Niks

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
Niks Jain
  • 1,617
  • 5
  • 27
  • 53

1 Answers1

0

Perhaps something like (untested):

import StringIO

output = StringIO.StringIO()
output.write(attachment[0].decode())

df_instance = PdfFileReader(file(output, "rb")) 

What is StringIO in python used for in reality?

http://effbot.org/librarybook/stringio.htm

Even if the above is not quite right, you should be able to make it work after reading a StringIO tutorial or two.

Community
  • 1
  • 1
Paul Collingwood
  • 9,053
  • 3
  • 23
  • 36