day.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:gthnk 作者: iandennismiller 项目源码 文件源码
def attach(self, binary):
        # determine the format of the file
        ext = puremagic.from_string(binary)

        page = None

        # if the attachment is a PDF
        if ext == ".pdf":
            # use PyPDF2 to read the stream
            pdf = PdfFileReader(BytesIO(binary))
            # if it is a multi-page PDF
            if pdf.getNumPages() > 1:
                # add the pages individually
                for pdf_page in pdf.pages:
                    output = PdfFileWriter()
                    output.addPage(pdf_page)

                    pdf_page_buf = BytesIO()
                    output.write(pdf_page_buf)
                    page = self.add_page(pdf_page_buf.getvalue())
            # if it is just a single page PDF
            else:
                # then add the original bytestream
                page = self.add_page(binary)
        # if the attachment is a recognized image
        elif ext in [".png", ".jfif", ".gif", ".jpeg", ".jpg"]:
            page = self.add_page(binary)
        # could not recognize file
        else:
            pass

        if page:
            return(page)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号