allow this function to export multiple images to word document docx file, currently it allow to export only one image to word, solve it.


// Export Images to Word
exportWordBtn.addEventListener('click', function() {
    if (images.length === 0) {
        alert('No images to export.');
        return;
    }
    
    // Load the docx library
    const script = document.createElement('script');
    script.src = 'https://cdn.jsdelivr.net/npm/docx@7.8.2/build/index.js';
    script.onload = function() {
        const { Document, Packer, Paragraph, ImageRun } = docx;

        const doc = new Document({
            sections: [] // Start with no sections to avoid blank page
        });

        const imagePromises = images.map(canvas => {
            return new Promise(resolve => {
                canvas.toBlob(blob => {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        resolve({
                            data: e.target.result,
                            width: canvas.width,
                            height: canvas.height
                        });
                    };
                    reader.readAsArrayBuffer(blob);
                }, 'image/png');
            });
        });

        Promise.all(imagePromises)
            .then(imageDatas => {
                imageDatas.forEach((imageData, index) => {
                    const maxWidth = 510; // A4 page width in points (excluding margins)
                    const maxHeight = 730; // A4 page height in points (excluding margins)
                    
                    let width = imageData.width;
                    let height = imageData.height;

                    // Scale image to fit within page boundaries
                    if (width > maxWidth || height > maxHeight) {
                        const ratio = Math.min(maxWidth / width, maxHeight / height);
                        width *= ratio;
                        height *= ratio;
                    }

                    // Only add a new section for images after the first one
                    if (index > 0) {
                        doc.addSection({
                            properties: {
                                page: {
                                    size: PageSize.A4,
                                    orientation: PageOrientation.PORTRAIT,
                                },
                            },
                        });
                    }

                    // Create a new paragraph with the image and add it to the document
                    const paragraph = new Paragraph({
                        children: [
                            new ImageRun({
                                data: imageData.data,
                                transformation: {
                                    width,
                                    height
                                }
                            })
                        ]
                    });
                    doc.addSection({
                        children: [paragraph]
                    });
                });

                return Packer.toBlob(doc);
            })
            .then(blob => {
                saveAs(blob, "exported_images.docx");
                statusElement.textContent = 'Images exported to Word successfully.';
            })
            .catch(error => {
                console.error('Error exporting to Word:', error);
                statusElement.textContent = 'Error exporting to Word. Check console for details.';
            });
    };
    script.onerror = function() {
        console.error('Error loading docx module');
        statusElement.textContent = 'Error loading Word export module. Check console for details.';
    };
    document.head.appendChild(script);
});