Ejemplo n.º 1
0
def test_fig_to_image():
    fig, ax = plt.subplots(dpi=72)
    ax.plot([0, 1], [1, 0], color="C2")
    text = ax.set_title("Hello, World!", fontsize=24)
    image = image_misc.to_image(fig)
    slide = pptx_misc.get_slide()
    pptx_misc.paste_image(slide, image)
Ejemplo n.º 2
0
    def compare(self, artist):
        """ Args:
        """
        fig = artist_misc.to_figure(artist)
        fig.set_dpi(72)
        slide_size = pptx_misc.get_slide_size(self.slide)

        # To know the size of image proactively.
        image = image_misc.to_image(artist)
        left1, top1, left2, top2 = _decide_positions(slide_size, image.size)

        rasterize(artist, slide=self.slide, left=left1, top=top1)
        transcribe(artist, slide=self.slide, left=left2, top=top2)
Ejemplo n.º 3
0
def rasterize(target, slide=None, **kwargs):
    """ Convert to ``PIL.Image``.
    and paste it to ``Slide``.

    Return: Shape.
    """
    slide = pptx_misc.get_slide(arg=slide)
    image = image_misc.to_image(target)
    left, top = PositionSolver(slide, image.size).configure(kwargs)
    shape = pptx_misc.paste_image(slide, image, left=left, top=top)

    # Post process.
    pptx_misc.select([shape])
    return shape
Ejemplo n.º 4
0
def text_converter(slide_editor, artist):
    patch = artist.get_bbox_patch()
    box = slide_editor.get_box(artist)
    if artist.get_rotation() != 0:
        raise NonHandlingException

    if matplotlib.cbook.is_math_text(artist.get_text()):
        if patch:
            patch_shape = fancybox.fancybbox_converter(slide_editor, patch)
        image = image_misc.to_image(artist)
        text_shape = pptx_misc.paste_image(slide_editor.slide,
                                           image,
                                           left=box.Left,
                                           top=box.Top)
        return [patch_shape, text_shape]

    if patch:
        shape = fancybox.fancybbox_converter(slide_editor, patch)
    else:
        shape = slide_editor.slide.Shapes.AddTextBox(
            constants.msoTextOrientationHorizontal, **box)
        # Subtract the margin of TextFrame so that the top and left should be equivalent.
        # I feel this simple logic is not enough...
        shape.Top -= shape.TextFrame.MarginTop
        shape.Left -= shape.TextFrame.MarginLeft

    if not patch:
        shape.TextFrame.Autosize = constants.ppAutoSizeShapeToFitText
    if artist.get_wrap():
        text = artist._get_wrapped_text()
    else:
        text = artist.get_text()
    shape.TextFrame.TextRange.Text = text
    shape.TextFrame.Textrange.Font.Size = artist.get_fontsize()
    shape.TextFrame.WordWrap = constants.msoFalse

    # Itatic
    style = artist.get_style()
    if style in {"italic", "oblique"}:
        shape.TextFrame.Textrange.Font.Italic = True

    # Color
    rgb = artist.get_color()
    # Is there a place to set ``alpha``?
    rgb_int, alpha = conversion_misc.to_color_infos(rgb)
    shape.TextFrame.TextRange.Font.Color.RGB = rgb_int

    return shape
Ejemplo n.º 5
0
def transcribe(target, slide=None, **kwargs):
    """Convert to Objects of PowerPoint.

    Return: list of shapes.
    """
    kwargs = {key.lower(): value for key, value in kwargs.items()}
    slide = pptx_misc.get_slide(arg=slide)

    left = kwargs.get("left", None)
    top = kwargs.get("top", None)
    if left is None or top is None:
        image = image_misc.to_image(target)
        left, top = PositionSolver(slide, image.size).configure(kwargs)

    transcriber = PPTXTranscriber(slide, left=left, top=top)
    shapes = transcriber.transcribe(target)
    if shapes:
        pptx_misc.select(shapes)
    return shapes
Ejemplo n.º 6
0
def to_image_shape(slide_editor, artists):
    """ Convert ``artist`` to ``Shape`` as ``PIL.Image``.

    Note
    ----------------------------------
    This function takes a long time especially when
    there are many aritists in figure of the the ``artist``.
    This function is expected to be called only a few.
    """
    if isinstance(artists, Artist):
        artists = [artists]
    boxes = [slide_editor.get_box(artist) for artist in artists]
    box = Box.union(boxes)
    # box = slide_editor.get_box(artist)
    image = image_misc.to_image(artists)
    shape = pptx_misc.paste_image(slide_editor.slide, image)
    print("box", box)
    for key, value in box.items():
        setattr(shape, key, value)
    return shape
Ejemplo n.º 7
0
def send(target, slide=None, separator="default", match=None, **kwargs):
    """Send `target` to PowerPoint.
    """

    kwargs = {key.lower(): value for key, value in kwargs.items()}
    slide = pptx_misc.get_slide(arg=slide)

    left = kwargs.get("left", None)
    top = kwargs.get("top", None)
    if left is None or top is None:
        image = image_misc.to_image(target)
        left, top = PositionSolver(slide, image.size).configure(kwargs)

    if match is not None:
        separator = FindobjSeparator(match)

    shape_artists, image_artists = SeparatorInterpreter(separator,
                                                        target).partition

    slide = pptx_misc.get_slide(arg=slide)

    if image_artists:
        image_shape = rasterize(image_artists, slide=slide, left=left, top=top)
    else:
        image_shape = None

    transcriber = PPTXTranscriber(slide, left=left, top=top, offset=target)
    shapes = transcriber.transcribe(shape_artists)

    if image_shape:
        grouped_shape = pptx_misc.group([image_shape, *shapes])
    else:
        grouped_shape = pptx_misc.group(shapes)
    if grouped_shape:
        pptx_misc.select([grouped_shape])
    return grouped_shape