liveBook · Manning

DocxLoader

As explained in Build AI-Enhanced Web Apps

DocxLoader is a LangChain.js document loader that parses DOCX files into document objects.

DocxLoader parses DOCX files into LangChain.js Document objects, preparing their content for chunking, summarization, or information retrieval. Import it from @langchain/community/document_loaders/fs/docx and run it in a Node.js or browser-based JavaScript application. section 7.1.3

Load a DOCX file

Select the loader from the file extension, instantiate it with the path, and call load(). The returned documents can then flow into a text splitter or summarization chain. section 7.1.3

Listing 7.1 Loading different document types
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf";    #1
import { DocxLoader } from "@langchain/community/document_loaders/fs/docx";
import path from "path";    #2

const loaders = {
  pdf: PDFLoader,
  docx: DocxLoader,
};

const loadDocumentFromFile = async (filePath) => {    #3
  const fileExtension = path.extname(filePath)
.substring(1).toLowerCase();
  const LoaderClass = loaders[fileExtension];

  if (!LoaderClass) {
    console.error(`No loader found for file extension: ${fileExtension}`);
    return null;
  }

  const loader = new LoaderClass(filePath);
  return await loader.load();
};

loadDocumentFromFile("./data/aesops-fables.pdf")    #4
  .then((documents) => {
    console.log(`Loaded ${documents.length} documents.`);
  })
  .catch((error) => {
    console.error("Error loading documents:", error);
  });

Use the complete loader pattern to support DOCX alongside PDF files, handle unsupported extensions, and report loading errors. section 7.1.3

How Build AI-Enhanced Web Apps covers it

How deep it goes
Explains imports, extension-based loader selection, loading, errors, and downstream chunking, but does not detail DOCX parsing internals.
Examples
Uses a JavaScript loader map that handles PDF and DOCX paths and logs the number of loaded documents.
What it assumes
Assumes JavaScript or TypeScript familiarity and introduces DOCX loading without requiring Python for core LangChain.js.
What you can do after
Apply it via LangChain.js to load DOCX files and pass the resulting documents into chunking or summarization workflows.
How it connects
Connects document loading with PDFLoader, Document objects, RecursiveCharacterTextSplitter, and MapReduce summarization.

This explainer was generated by Manning's knowledge graph from the text of Build AI-Enhanced Web Apps. The section links point to the parts of the book it draws on.