singerla / pptx-automizer

A template based pptx generator for Node.js
MIT License
68 stars 12 forks source link

On presentation creation, I got the error "Could not find file ppt/slides/slide84225942.xml" #123

Closed Suniron closed 1 month ago

Suniron commented 1 month ago

Hello,

I can't create a new presentation: I got an error about Could not find file ppt/slides/slide84225942.xml.

As the example below, I tried two ways to create the .pptx.

In my case, I want to replace texts and images from one template, I haven't found a simpler way to make the replacements in this single source PowerPoint file.

💡 I think wee need a simple example to do this, in the documentation.

Feel free to correct me 😅

My code:

import fs from 'node:fs'
import path from 'node:path'
import Automizer, { modify } from 'pptx-automizer'

const automizer = new Automizer({
  outputDir: path.resolve(__dirname),
  removeExistingSlides: true,
})

export const generatePresentation = async (companyName: string) => {
  performance.mark('start-generatePresentation')

  // = Fill the template =
  const rootTemplate = fs.readFileSync(
    path.resolve(__dirname, '../templates/my_template.pptx'),
    // 'binary',
  )
  const presentation = automizer.loadRoot(rootTemplate).load(rootTemplate, 'root')
  const presentationInfo = await presentation.getInfo()
  const totalSlides = presentationInfo.slidesByTemplate('root')

  for (const slide of totalSlides) {
    presentation.addSlide('root', slide.id, (s) => {
      s.modifyElement('replaceText', modify.replaceText([
        { by: { text: `Hello the company ${companyName}` }, replace: 'company_name' },
      ]))
    })
  }

  // = Save the presentation =
  // Solution 1: not work "Could not find file ppt/slides/slide84225942.xml", and crash
  const finalJSZip = await presentation.getJSZip()
  const buf = await finalJSZip.generateAsync({ type: 'nodebuffer' })
  fs.writeFileSync(path.resolve(__dirname, 'my_updated_file-solution-1.pptx'), buf)

  // ...or Solution 2: not work "Could not find file ppt/slides/slide84225942.xml", and crash
  await presentation.write('my_updated_file-solution-2.pptx')
}

generatePresentation('My company name')

Thanks in advance 🙏

singerla commented 1 month ago

Hi! I guess you might need slide.number instead of slide.id as second argument for presentation.addSlide.

You could also try to .loadRoot and .load the files by using a string to simplify if templateDir is set properly.

Please let me know if this helps!

Suniron commented 1 month ago

Oh thanks, it seems to be better 😅.