秃头小妹0 2021-08-17 01:39 采纳率: 50%
浏览 20
已结题

为什么我在多个图片转换PDF时,只能保存于一次吗


import os

import PIL
from reportlab.lib import utils
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas

from image_split import ClipType
from reportlab.lib.pagesizes import A4, landscape, portrait
from PIL import Image
from reportlab.platypus import SimpleDocTemplate, Image, PageBreak
from reportlab.platypus import PageTemplate, Frame


# ----------------------------------------------------------------------

def create_pdf(filename, path, page, cliptype, imageformat):
    """
    """
    if cliptype == ClipType.ALL:
        imgDoc = canvas.Canvas(filename)  # pagesize=letter
        imgDoc.setPageSize(A4)
        document_width, document_height = A4
    for page in range(page):
        print 'create_pdf', page
        image_file = PIL.Image.open(os.path.join(path, '{0}.{1}.{2}'.format(page, '1', imageformat)))
        image_width, image_height = image_file.size
        if not (image_width > 0 and image_height > 0):
            raise Exception
        image_aspect = image_height / float(image_width)
        print_width = document_width
        print_height = document_width * image_aspect
        imgDoc.drawImage(ImageReader(image_file), document_width - print_width,
                         document_height - print_height, width=print_width,
                         height=print_height, preserveAspectRatio=True)
        # Story.append(Image(image_file, width, height))
        imgDoc.showPage()
        imgDoc.save()
    print "%s created" % filename


# ----------------------------------------------------------------------
if __name__ == "__main__":
    cliptype = ClipType.MARGIN_ONLY
    pdfsavepath = r".\tmp\pdf\484d8037a509648a8e09fc0466b06f38\tongjixuexifangfa{0}.pdf".format(cliptype)
    subimagepath = r".\tmp\pdf\484d8037a509648a8e09fc0466b06f38\subimage{0}".format(cliptype)
    create_pdf(pdfsavepath, subimagepath, 20, cliptype, 'jpg')

img

展开全部

  • 写回答

1条回答 默认 最新

  • 程序媛一枚~ Python领域新星创作者 2021-08-17 03:15
    关注

    是的,要想写入多张照片,把第40行代码 imgDoc.save()移到for循坏外就可以了哦~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    秃头小妹0 2021-08-17 04:03

    对对付,蟹蟹蟹蟹

    回复
编辑
预览

报告相同问题?

问题事件

  • 系统已结题 9月3日
  • 已采纳回答 8月27日
  • 创建了问题 8月17日
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部