OpenXML - 获取图像Alt文本标题

时间:2015-08-14 11:30:11

标签: c# openxml openxml-sdk

我试图使用OpenXML在PowerPoint演示文稿中迭代图像。

我已经研究过如何做到这一点。

我没有尝试获取图像Alt-Text标题....

这是我的代码:

List<ImagePart> imageParts = new List<ImagePart>();

part.GetPartsOfType<ImagePart>(imageParts);

foreach (ImagePart imagePart in imageParts)
{
    if (imagePart != null)
    {

    // Get the Relationship Id
    string oldRelID = part.GetIdOfPart(imagePart);

    // Get the Alt-Text Tile relating to this image

    }
}

那里有哪些OpenXML专家可以给我一些指示?

由于

更新:

我已尝试迭代XML,但当幻灯片上有多个图像时,我得到的关联图像的标题值不正确。

我想我需要能够使用ImagePart Id然后找到相应的标题

以下代码无法获得标题......

foreach (ImagePart imagePart in imageParts)
{
    string mapReference = "";

    XmlNode thisNode = pictureNodeList[imageCounter];

    foreach (XmlNode xmlnode in thisNode)
    {
        foreach (XmlNode xmlchildnode in xmlnode)
        {
            foreach (XmlAttribute att in xmlchildnode.Attributes)
            {
                if (att.Name == "title")
                {
                    mapReference = att.Value;
                    imageCounter += 1;
                 }
            }
         }
    }
}

1 个答案:

答案 0 :(得分:4)

你有90%的路要走。

您需要找到其Blip属性与您拥有的Embed的ID匹配的ImagePart元素。 Blip包含在BlipFill中,Picture又包含在Picture元素中。 pic(XML中的NonVisualPictureDrawingProperties)元素有一个nvPicPr元素(NonVisualDrawingProperties),后者又有一个cNvPr元素(<p:pic> <p:nvPicPr> <p:cNvPr id="4" name="Picture 3" descr="My Description" title="My Title" /> <p:cNvPicPr> <a:picLocks noChangeAspect="1" /> </p:cNvPicPr> <p:nvPr /> </p:nvPicPr> <p:blipFill> <a:blip r:embed="rId2" cstate="print"> <a:extLst> <a:ext uri="{28A0092B-C50C-407E-A947-70E740481C1C}"> <a14:useLocalDpi xmlns:a14="http://schemas.microsoft.com/office/drawing/2010/main" val="0" /> </a:ext> </a:extLst> </a:blip> <a:stretch> <a:fillRect /> </a:stretch> </p:blipFill> <p:spPr> <a:xfrm> <a:off x="7260298" y="5445224" /> <a:ext cx="1883701" cy="1412776" /> </a:xfrm> <a:prstGeom prst="rect"> <a:avLst /> </a:prstGeom> </p:spPr> </p:pic> )它在那里,你会找到标题。例如,您的XML可能如下所示:

Title

以下代码将输出每个图像的Picture属性。请注意,由于您拥有强类型Description对象,因此您也可以轻松访问其他属性(例如using (PresentationDocument doc = PresentationDocument.Open(filename, false)) { //get the first slide SlidePart part = doc.PresentationPart.SlideParts.First(); //get all ImageParts in the first slide List<ImagePart> imageParts = new List<ImagePart>(); part.GetPartsOfType<ImagePart>(imageParts); foreach (ImagePart imagePart in imageParts) { //find the picture related to the image Picture pic = part.Slide.Descendants<Picture>().Where(p => p.BlipFill.Blip.Embed == part.GetIdOfPart(imagePart)).FirstOrDefault(); //Output the Title property Console.WriteLine(pic.NonVisualPictureProperties.NonVisualDrawingProperties.Title); } } )。

#! /usr/bin/env python

import os
import random
import pygame
import math
import sys

os.environ["SDL_VIDEO_CENTERED"] = "1"

winw = 600
winh = 600
screen = pygame.display.set_mode((winw, winh))
pygame.display.set_caption(" Charger V1 Lvl1")

clock = pygame.time.Clock()

player1 = pygame.rect.Rect(32, 32, 24, 24)
rect1 = pygame.rect.Rect(550, 550, 16, 16)
rect2 = pygame.rect.Rect(500, 850, 16, 16)

class Player(object):
    def __init__(self):
        self.rect = player1

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 1
        if key[pygame.K_LEFT]:
            self.rect.move_ip(-2, 0)
        if key[pygame.K_RIGHT]:
            self.rect.move_ip(2, 0)
        if key[pygame.K_UP]:
           self.rect.move_ip(0, -2)
        if key[pygame.K_DOWN]:
           self.rect.move_ip(0, 2)

    def draw(self, surface):
        pygame.draw.rect(surface, (0, 0, 128), self.rect)

class Lvl1a(object):
    def __init__(self):
        self.rect = rect1
        self.x = 0
        self.y = 0

    def move(self):
        self.rect.move_ip(0, -1)

    def draw(self, surface):
        pygame.draw.rect(surface, (100, 100, 100), self.rect)

class Lvl1b(object):
    def __init__(self):
        self.rect = rect2
        self.x = 0
        self.y = 0

    def move(self):
        self.rect.move_ip(0, -1)

    def draw(self, surface):
        pygame.draw.rect(surface, (100, 100, 100), self.rect)

pygame.init()

player = Player()
clock = pygame.time.Clock()
lvl1a = Lvl1a()
lvl1b = Lvl1b()

running = True       
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
           break
           running = False

    screen.fill((255, 255, 255))

    lvl1b.draw(screen)
    lvl1b.move()
    lvl1a.draw(screen)
    player.draw(screen)
    lvl1a.move()
    player.handle_keys()
    pygame.display.update()

    clock.tick(40)