-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PR for inserting arbitrary PNG #62
Comments
This would be helpful for my projects, too. I could get rid of maintaining image collections in pptx, but use images from a directory. 1.) will be easy to implement though; I think 4.) is the way with least effort/highest gain; |
My bad phrasing; the selector (id/name) of the image element.
It's how its done in docx templater, when I say modify that's just logically, implementation may be to remove the text element and add an image element in its place.
yes agreed, though it adds a dependency and requires the user to know about X/Y positioning. We could always come back later and add a method of getting x/y position of {replaceElement} I suppose to enable vague compatibility with replace style templating. |
I also faced a case when I need to replace an image on the template, but I follow an example in the documentation. My current solution is to have a separate slide with assets/images inside to use them when needed by ElementSelector. But there are a few problems:
So from my point of view, it would be great to have the possibility:
It's not something urgent, but I believe that it could be a good functionality for the library. Many Thanks! |
Yes, I totally agree! I can confirm this can be painful to sail around. I need to do more investigation on this! |
Hello, I am trying to replace a few images with images from my disk. Using pptxgenjs I created a .pptx containing the images (as a library), something like :
And then I can use the image using the automizer :
But what I ultimately want is to replace an existing image with this new one so it will inherit ppt animations, size, position, etc. How should I do that ? Thanks! |
Not my library, but AKAIK there is no 'native' way of doing this here. What you describe is basically Implementation suggestion 1 or 2 above. It should be a pretty tiny PR, more or less you really just need to add the new image to the folder, and then update the image element to refer to the new image, then finally delete the old image media. |
This is an interesting approach, but i'm afraid, what you need it is not implemented yet. I could imagine something like this: let presentation = automizer
.loadRoot("empty.pptx")
.load("full.pptx")
.load("images.pptx")
.addSlide("empty.pptx", 1, async function (slide) {
// add the externally created image to a temporary slide
// to have it in your output presentation
slide.addElement("images.pptx", 1, "My Image");
// The created media target of this image needs to be
// made available for other operations.
});
.addSlide("full.pptx", 1, async function (slide) {
// modify animated image
slide.modifyElement("My animated image", [
// Todo is `modify.updateRelationTarget`:
modify.updateRelationTarget(*/ get created media target from "My Image" /*)
]);
return slide;
}); It is basically what @MP70 figured out, but you will need some information about the created contents during runtime. Some caveats could be:
You can take a look at <Relationship Target="../media/image6.png" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Id="rId3"/> We need to "simply" update Target for the corresponding So, a more elegant approach would be an image modifier to do this in one go, just like: let presentation = automizer
.loadRoot("empty.pptx")
.load("full.pptx")
.load("images.pptx")
.addSlide("full.pptx", 1, async function (slide) {
// mody animated image
slide.modifyElement("My animated image", [
// This could be a shortcut:
modify.updateTarget("images.pptx", 1, "My Image")
]);
return slide;
}); I need to investigate this more detailed! 🤔 |
Referring to @andrew-t-adaas suggestion, it would be an improvement to add an image directly, without the need to build a .pptx around first. let presentation = automizer
.loadRoot("empty.pptx")
.load("full.pptx")
.addSlide("full.pptx", 1, async function (slide) {
// mody animated image
slide.modifyElement("My animated image", [
// Read from file:
modify.updateMedia("/path/to/image.jpg")
]);
return slide;
}); which will be no big thing to implement. But we need to register each new image extension once in |
In case it helps someone, We opted for the following post processing workaround : First we keep track of the images we would like to replace :
When the ppt is done we replace the images in the resulting zip file (work in progress, should be optimized by slide) :
Quite ugly but it works :) |
Update: It is now possible to load and apply an external media file by modifier. @labe-me Thanks a lot for your code snippet! Based on this simple and effective inspiration, I have implemented a generic solution to import files to const automizer = new Automizer({
// ...
// specify a fallback dir to import media files from
mediaDir: `${__dirname}/../__tests__/media`,
});
const pres = automizer
.loadRoot(`RootTemplate.pptx`)
// load one or more files from mediaDir
.loadMedia([`feather.png`, `test.png`], /* or use a custom dir */)
.load(`SlideWithShapes.pptx`, 'shapes')
.load(`SlideWithImages.pptx`, 'images');
pres.addSlide('shapes', 1, (slide) => {
slide.addElement('images', 2, 'imagePNG', [
// directly override "Target" attribtue of recently created relation for 'imagePNG'
ModifyImageHelper.setRelationTarget('test.png'),
// eventually update size
ModifyShapeHelper.setPosition({
w: CmToDxa(5),
h: CmToDxa(3),
}),
]);
}); Please also refer to add-external-image.test.ts. |
@singerla Nice! Can the setRelationTarget() modifier be used on an existing slide's image too?
|
No, currently it does only work on added images... Needs some tweaks 😃 |
Please check out v0.4.0 to use @andrew-t-adaas: Modified images will not be moved to top layer any more. |
This could now be done with help of beautiful PptxGenJS |
Sorry, I know I've just added a PR, not expecting a response soon just thought I'd add it while its on my mind..
Other libs such as node-pptx allow the user to add an arbitrary image in whereas this seems to require the image is already on a template slide. I would like the ability to extend templating (in spirit) to images, like how we have replaceText which takes a text element ID maybe we have a 'replaceImage' which will take a imageElement ID and copy the new image into ppt/images, remove the old image and update the reference in the element. This will be trivial to implement BUT the issue is that it is going to be painful for the user to find the Image element ID in order to pass it in replaceImage.
so could/should we:
The text was updated successfully, but these errors were encountered: