예제 #1
0
 def test__scale_calculates_correct_dimensions(self):
     """Image._scale() calculates correct dimensions"""
     # setup ------------------------
     test_cases = (((None, None), (Px(204), Px(204))), ((1000, None),
                                                        (1000, 1000)),
                   ((None, 3000), (3000, 3000)), ((3337, 9999), (3337,
                                                                 9999)))
     partname = PackURI('/ppt/media/image1.png')
     image = Image.new(partname, test_image_path)
     # verify -----------------------
     for params, expected in test_cases:
         width, height = params
         assert image._scale(width, height) == expected
예제 #2
0
    def _scale(self, width, height):
        """
        Return scaled image dimensions based on supplied parameters. If
        *width* and *height* are both |None|, the native image size is
        returned. If neither *width* nor *height* is |None|, their values are
        returned unchanged. If a value is provided for either *width* or
        *height* and the other is |None|, the dimensions are scaled,
        preserving the image's aspect ratio.
        """
        native_width_px, native_height_px = self._size
        native_width = Px(native_width_px)
        native_height = Px(native_height_px)

        if width is None and height is None:
            width = native_width
            height = native_height
        elif width is None:
            scaling_factor = float(height) / float(native_height)
            width = int(round(native_width * scaling_factor))
        elif height is None:
            scaling_factor = float(width) / float(native_width)
            height = int(round(native_height * scaling_factor))
        return width, height
예제 #3
0
class DescribeImagePart(object):

    def it_can_construct_from_an_image_object(self, new_fixture):
        package_, image_, _init_, partname_ = new_fixture

        image_part = ImagePart.new(package_, image_)

        package_.next_image_partname.assert_called_once_with(image_.ext)
        _init_.assert_called_once_with(
            partname_, image_.content_type, image_.blob, package_,
            image_.filename
        )
        assert isinstance(image_part, ImagePart)

    def it_provides_access_to_its_image(self, image_fixture):
        image_part, Image_, blob, desc, image_ = image_fixture
        image = image_part.image
        Image_.assert_called_once_with(blob, desc)
        assert image is image_

    def it_can_scale_its_dimensions(self, scale_fixture):
        image_part, width, height, expected_values = scale_fixture
        assert image_part.scale(width, height) == expected_values

    def it_knows_its_pixel_dimensions(self, size_fixture):
        image, expected_size = size_fixture
        assert image._px_size == expected_size

    # fixtures -------------------------------------------------------

    @pytest.fixture
    def image_fixture(self, Image_, image_):
        blob, filename = 'blob', 'foobar.png'
        image_part = ImagePart(None, None, blob, None, filename)
        return image_part, Image_, blob, filename, image_

    @pytest.fixture
    def new_fixture(self, request, package_, image_, _init_):
        partname_ = package_.next_image_partname.return_value
        return package_, image_, _init_, partname_

    @pytest.fixture(params=[
        (None, None, Px(204), Px(204)),
        (1000, None, 1000,    1000),
        (None, 3000, 3000,    3000),
        (3337, 9999, 3337,    9999),
    ])
    def scale_fixture(self, request):
        width, height, expected_width, expected_height = request.param
        with open(test_image_path, 'rb') as f:
            blob = f.read()
        image = ImagePart(None, None, blob, None)
        return image, width, height, (expected_width, expected_height)

    @pytest.fixture
    def size_fixture(self):
        with open(test_image_path, 'rb') as f:
            blob = f.read()
        image = ImagePart(None, None, blob, None)
        return image, (204, 204)

    # fixture components ---------------------------------------------

    @pytest.fixture
    def Image_(self, request, image_):
        return class_mock(
            request, 'pptx.parts.image.Image', return_value=image_
        )

    @pytest.fixture
    def image_(self, request):
        return instance_mock(request, Image)

    @pytest.fixture
    def _init_(self, request):
        return initializer_mock(request, ImagePart)

    @pytest.fixture
    def package_(self, request):
        return instance_mock(request, Package)
예제 #4
0
img_path = '%s/dashboard2.png' % mypath

prs = Presentation()

# --------------------------------------------------------------
blank_slidelayout = prs.slidelayouts[6]

slide = prs.slides.add_slide(blank_slidelayout)

top = Inches(1)
left = Inches(0.5)

pic = slide.shapes.add_picture(img_path, left, top)

width = Px(650)
height = int(width*wpercent)
pic = slide.shapes.add_picture(img_path, left, top, width, height)

# --------------------------------------------------------------
title_only_slidelayout = prs.slidelayouts[5]
slide = prs.slides.add_slide(title_only_slidelayout)
shapes = slide.shapes

shapes.title.text = 'Adding a Table'

cols = 2
rows = 4
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)
예제 #5
0
def create_pptx(region_list, caluculateion_type='live'):
    img_basepath = 'C:/workspace/py/MyPythonApp/src/viewingScore/create_graph/%s/' % caluculateion_type
    SLD_LAYOUT_TITLE_AND_CONTENT = 1

    gender_list = ('男性', '女性')
    week_list = ('', 'weekday_', 'weekend_')
    time_list = ('5-9', '9-19', '19-23', '23-29')
    img_gender_name_list = ('r_m_', 'r_fm_')
    img_name_tips = ('20to34', '35to49', 'over50')
    #'output_'+region+
    prs = Presentation()
    for region in region_list:
        # create slide to describe region
        blank_slide_layout = prs.slide_layouts[0]
        slide = prs.slides.add_slide(blank_slide_layout)
        shapes = slide.shapes

        title_shape = shapes.title
        body_shape = shapes.placeholders[1]

        title_shape.text = region  # title

        # create slide with graph to describe hole users having properties at that region
        blank_slide_layout = prs.slide_layouts[1]
        slide = prs.slides.add_slide(blank_slide_layout)
        shapes = slide.shapes

        title_shape = shapes.title
        body_shape = shapes.placeholders[1]
        title_shape.text = region + '_個人全体'  # title

        # paste image
        width = height = Px(600)

        base_left = 1.8
        base_top = 1.25

        img_path = unicode(img_basepath + 'output_' + region +
                           '_r_individuals.png')
        print img_path
        left = Inches(base_left)
        top = Inches(base_top)
        pic = slide.shapes.add_picture(img_path, left, top, width, height)

        for _week in week_list:
            if _week == '':
                for i, gender in enumerate(gender_list):
                    blank_slide_layout = prs.slide_layouts[
                        SLD_LAYOUT_TITLE_AND_CONTENT]
                    slide = prs.slides.add_slide(blank_slide_layout)
                    shapes = slide.shapes

                    title_shape = shapes.title
                    body_shape = shapes.placeholders[1]

                    title_shape.text = region + '_' + gender  # title

                    # paste image
                    img_width = img_height = Px(300)
                    # image base
                    base_left = 0.5
                    base_top = 2
                    # text base
                    txt_base_left = 1.2
                    txt_base_top = 1.4
                    for img_idx in xrange(len(img_name_tips)):
                        left_inch = base_left + img_idx * 3
                        top_inch = base_top + img_idx * 1

                        img_path = unicode(img_basepath + 'output_' + region +
                                           '_' + _week +
                                           img_gender_name_list[i] +
                                           img_name_tips[img_idx] + '.png')
                        left = Inches(left_inch)
                        top = Inches(top_inch)
                        print img_path
                        pic = slide.shapes.add_picture(img_path, left, top,
                                                       img_width, img_height)

                        # write text
                        left_inch = txt_base_left + img_idx * 3
                        top_inch = txt_base_top + img_idx * 1

                        left = Inches(left_inch)
                        top = Inches(top_inch)
                        width = height = Inches(1)
                        txBox = slide.shapes.add_textbox(
                            left, top, width, height)
                        tf = txBox.textframe
                        p = tf.add_paragraph()
                        p.text = gender + img_name_tips[img_idx]
                        p.font.size = Pt(24)
                        p.font.bold = True

            for _time in time_list:
                if _week != '':
                    for i, gender in enumerate(gender_list):
                        blank_slide_layout = prs.slide_layouts[
                            SLD_LAYOUT_TITLE_AND_CONTENT]
                        slide = prs.slides.add_slide(blank_slide_layout)
                        shapes = slide.shapes

                        title_shape = shapes.title
                        body_shape = shapes.placeholders[1]

                        title_shape.text = region + '_' + _week + _time + '_' + gender  # title

                        # paste image
                        img_width = img_height = Px(300)
                        # image base
                        base_left = 0.5
                        base_top = 2
                        # text base
                        txt_base_left = 1.2
                        txt_base_top = 1.4
                        for img_idx in xrange(len(img_name_tips)):
                            left_inch = base_left + img_idx * 3
                            top_inch = base_top + img_idx * 1

                            img_path = unicode(img_basepath + 'output_' +
                                               region + '_' + _week +
                                               img_gender_name_list[i] +
                                               img_name_tips[img_idx] + '_' +
                                               _time + '.png')
                            left = Inches(left_inch)
                            top = Inches(top_inch)
                            print img_path
                            pic = slide.shapes.add_picture(
                                img_path, left, top, img_width, img_height)

                            # write text
                            left_inch = txt_base_left + img_idx * 3
                            top_inch = txt_base_top + img_idx * 1

                            left = Inches(left_inch)
                            top = Inches(top_inch)
                            width = height = Inches(1)
                            txBox = slide.shapes.add_textbox(
                                left, top, width, height)
                            tf = txBox.textframe
                            p = tf.add_paragraph()
                            p.text = gender + img_name_tips[img_idx]
                            p.font.size = Pt(24)
                            p.font.bold = True
    prs.save('%s.pptx' % caluculateion_type)
예제 #6
0
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 29 12:07:20 2014

@author: ecksjoh
"""

from pptx import Presentation
from pptx.util import Inches, Px

img_path = 'monty-truth.png'

prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)

left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)

left = Inches(5)
width = Px(280)
height = int(width * 1.427)
pic = slide.shapes.add_picture(img_path, left, top, width, height)

prs.save('test.pptx')