我正在尝试获取 Word 文档中的图像集合。此页面的文档:https ://dev.office.com/reference/add-ins/word/inlinepicture 从字面上看是示例的剪切粘贴,实际上并未显示如何获取图像 - 只有第一。
每张图片我需要以下内容:
任何格式的数据都可以。我看到有一种getBase64ImageSrc
方法 - 这会做。- 文件名
没有文件名很好 - 我可以看到 API 没有它 - 我可以使用 alt 文本或只是image_{n}
在 {n} 是图像索引的地方构建它,但我看不到获取扩展名的方法 - 这是在数据作为data:image/jpeg;blahblah
???我不知道文档没有这种级别的信息。
到目前为止,我有以下代码,但我真的不确定它是否会起作用:
Word.run(
async (context) =>
{
// Create a proxy object for the pictures.
const allPictures = context.document.body.inlinePictures;
// Queue a command to load the pictures
context.load(allPictures);
// Synchronize the document state by executing the queued commands,
// and return a promise to indicate task completion.
return context.sync().then(() => allPictures);
})
.then((allPictures) =>
{
const images: IFileData[] = [];
let picture: Word.InlinePicture | undefined;
let imageCount = 0;
while (undefined !== (picture = allPictures.items.pop()))
{
const data = picture.getBase64ImageSrc();
const extension = ""; // TODO: no idea how to find this
const filename =
(
Strings.isNullOrEmpty(picture.altTextTitle)
? `image_${imageCount++}`
: Path.toFriendlyUrl(picture.altTextTitle)
)
images.push({
filename: filename + extension,
data: data
});
}
resolve(images);
})
.catch((e) => reject(e));
我在这里使用了一些自定义助手,它们执行以下操作:
- Strings.isNullOrEmpty
如果字符串为空或空则返回真,否则返回假 - Path.toFriendlyUrl
返回带有空格转换的字符串-
和一些其他改进
我目前的方法正确吗?