Forum Discussion
Ross_Cullen
May 28, 2024Copper Contributor
What to do with document "content" from docx file
Hello, I can successfully connect to and call a Graph API to retrieve a document from a sharepoint document library using https://graph.microsoft.com/v1.0/sites/{siteId}/drives/{driveId}/items...
Ross_Cullen
May 28, 2024Copper Contributor
Replying for anyone in the future, as I found a solution for what I need and may help others
const myDocument = "call to graph api that returns the content of the docx file"
public static async ExtractDocumentTextContent(myDocument) {
const mammoth = require('mammoth');
try {
const chunks: Buffer[] = [];
for await (const chunk of myDocument) {
chunks.push(chunk);
}
const buffer = Buffer.concat(chunks);
const result = await mammoth.extractRawText({ buffer });
const textContent = result.value;
return textContent;
} catch (error) {
console.error('Error extracting document content:', error);
}
}
This returns the textContent of the docx file, which I can then use in assertions/expects
expect(textContent).toContains("My expected string");
const myDocument = "call to graph api that returns the content of the docx file"
public static async ExtractDocumentTextContent(myDocument) {
const mammoth = require('mammoth');
try {
const chunks: Buffer[] = [];
for await (const chunk of myDocument) {
chunks.push(chunk);
}
const buffer = Buffer.concat(chunks);
const result = await mammoth.extractRawText({ buffer });
const textContent = result.value;
return textContent;
} catch (error) {
console.error('Error extracting document content:', error);
}
}
This returns the textContent of the docx file, which I can then use in assertions/expects
expect(textContent).toContains("My expected string");