Coder Social home page Coder Social logo

Comments (4)

mateobv07 avatar mateobv07 commented on May 23, 2024 1

I got the same error, this worked for me:

patchDocument(fs.readFileSync("input.docx"), {
  outputType: "nodebuffer",
  patches: {
    test: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun("test")],
    },
  },
}).then((doc) => {
  fs.writeFileSync("output.docx", doc);
});

The only problem is that patcher doesn't patch all the keys if the same key is multiple times in the same paragraph doing it this way. Then the fix to that is running it over the same file multiple time: #2267 (comment)

from docx.

ZergHeureux avatar ZergHeureux commented on May 23, 2024

Same issue here !
Have some short example :

    const file = fs.readFileSync(__dirname+"/../../templates/document.docx")
    const doc = await patchDocument({
        outputType: "nodebuffer",
        data: file,
        patches: {
            version: {
                type: PatchType.PARAGRAPH,
                children: [new TextRun("V5555")],
            }
        }
    })
    fs.writeFileSync("C:\\PATH\\demo.docx",doc)

The error returned by Node :


*\node_modules\docx\build\index.cjs:16888
}(e3))), e3) : u.Promise.reject(new Error("Can't read the data of '" + r2 + "'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?"));

Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?

from docx.

ZergHeureux avatar ZergHeureux commented on May 23, 2024

Maybe there is another way of editing the docx ?
Like rewrite the Buffer before paking, reading the ByteArray and comparing it with a template to catch identical Bytes and replacing it with the content we are looking for.

But it would be very time consuming and a bit complicated tho :)

from docx.

ZergHeureux avatar ZergHeureux commented on May 23, 2024

I got the same error, this worked for me:

patchDocument(fs.readFileSync("input.docx"), {
  outputType: "nodebuffer",
  patches: {
    test: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun("test")],
    },
  },
}).then((doc) => {
  fs.writeFileSync("output.docx", doc);
});

The only problem is that patcher doesn't patch all the keys if the same key is multiple times in the same paragraph doing it this way. Then the fix to that is running it over the same file multiple time: #2267 (comment)

This worked for me too, replacing ALL occurences by using it in a non-promise way :

    const doc = await patchDocument(fs.readFileSync("example.docx"), {
        outputType: "nodebuffer",
        patches: {
            version: {
                type: PatchType.PARAGRAPH,
                children: [new TextRun("V5555")],
            },
        },
      });
    fs.writeFileSync("demo.docx",doc)

However it doesnt apply the correspondant style to the new text. (Not sure its part of the patchers usage)

from docx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.