Edits history of script submission #23548 for ' Fill Word template (ms_word)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import * as wmill from "windmill-client"
    import { S3Object } from "windmill-client"
    import Docxtemplater from "docxtemplater"
    import PizZip from "pizzip"
    
    /**
     * Fill Word template
     * Fill a .docx template from Windmill object storage with `data` and write the result back to object storage, returning its S3 reference. The template uses docxtemplater tags: `{name}` for a value, `{#items}...{/items}` to repeat a block per array element or show it when truthy, `{^items}...{/items}` when empty. A tag with no matching key is left blank. Needs no Microsoft connection and works with any workspace object storage; for a PDF, upload the result with the OneDrive Upload File script and download it with `convert_to_pdf`.
     */
    export async function main(
      template: S3Object,
      data: { [key: string]: any },
      destination: S3Object
    ) {
      const bytes = await wmill.loadS3File(template)
      if (!bytes) throw new Error("template not found in object storage")
    
      const doc = new Docxtemplater(new PizZip(bytes), {
        paragraphLoop: true,
        linebreaks: true,
        nullGetter: () => "",
      })
      doc.render(data)
    
      await wmill.writeS3File(
        destination,
        new Blob([doc.getZip().generate({ type: "uint8array" })])
      )
      return destination
    }
    

    Submitted by hugo989 5 hours ago