读取邮件附件并在pyPdf2中使用:PdfFileReader

0 投票
1 回答
565 浏览
提问于 2025-04-18 16:57

我只是想在内存中读取邮件附件(附加的PDF文件),这样我就可以用它来处理pyPdf2

这是基本的代码:

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

在这里,我想知道如何通过attachment_contentattachment_name这两个变量来获取附加的PDF文件有多少页。

更多信息是,pyPdf2里有一个方法,可以用来获取页数。

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()

有没有人能帮我使用PdfFileReader

谢谢。

Niks

1 个回答

0

也许可以试试下面这个(没测试过):

import StringIO

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

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

Python中的StringIO到底有什么用?

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

即使上面的内容不完全正确,读一读关于StringIO的教程,你应该能搞定它。

撰写回答