Extracting text from files of various type including html, pdf, doc, docx, xls, xlsx, csv, pptx, png, jpg, gif, rtf, text/*, and various open office.


A text extraction node module.

Currently Extracts...

  • Markdown
  • EPUB
  • XML, XSL
  • PDF
  • ODT, OTT (experimental, feedback needed!)
  • RTF
  • CSV
  • ODS, OTS
  • ODP, OTP
  • ODG, OTG
  • DXF
  • application/javascript
  • All text/*mime-types.

In almost all cases above, what textract cares about is the mime type. So .htmland .htm, both possessing the same mime type, will be extracted. Other extensions that share mime types with those above should also extract successfully. For example, application/vnd.ms-excelis the mime type for .xls, but also for 5 other file types.

Does textract not extract from files of the type you need?Add an issue or submit a pull request. It many cases textract is already capable, it is just not paying attention to the mime type you may be interested in.


npm install textract

Extraction Requirements

Note, if any of the requirements below are missing, textract will run and extract all files for types it is capable. Not having these items installed does not prevent you from using textract, it just prevents you from extracting those specific files.

  • PDFextraction requires pdftotextbe installed, link
  • DOCextraction requires antiwordbe installed, link, unless on OSX in which case textutil (installed by default) is used.
  • RTFextraction requires unrtfbe installed, link, unless on OSX in which case textutil (installed by default) is used.
  • PNG, JPGand GIFrequire tesseractto be available, link. Images need to be pretty clear, high DPI and made almost entirely of just text for tesseractto be able to accurately extract the text.
  • DXFextraction requires drawingtotextbe available, link


Configuration can be passed into textract. The following configuration options are available

  • preserveLineBreaks: When using the command line this is set to trueto preserve stdout readability. When using the library via node this is set to false. Pass this in as trueand textract will not strip any line breaks.
  • preserveOnlyMultipleLineBreaks: Some extractors, like PDF, insert line breaks at the end of every line, even if the middle of a sentence. If this option (default false) is set to true, then any instances of a single line break are removed but multiple line breaks are preserved. Check your output with this option, though, this doesn't preserve paragraphs unless there are multiple breaks.
  • exec: Some extractors (dxf) use node's execfunctionality. This setting allows for providing config to execexecution. One reason you might want to provide this config is if you are dealing with very large files. You might want to increase the execmaxBuffersetting.
  • [ext].exec: Each extractor can take specific exec config. Keep in mind many extractors are responsible for extracting multiple types, so, for instance, the odtextractor is what you would configure for odtand odg/odtetc. Check the extractorsto see which you want to specifically configure. At the bottom of each is a list of typesfor which the extractor is responsible.
  • tesseract.lang: A pass-through to tesseract allowing for setting of language for extraction. ex: { tesseract: { lang:"chi_sim" } }
  • tesseract.cmd: tesseract.langallows a quick means to provide the most popular tesseract option, but if you need to configure more options, you can simply pass cmd. cmdis the string that matches the command-line options you want to pass to tesseract. For instance, to provide language and psm, you would pass { tesseract: { cmd:"-l chi_sim -psm 10" } }
  • pdftotextOptions: This is a proxy options object to the library textract uses for pdf extraction: pdf-text-extract. Options include ownerPassword, userPasswordif you are extracting text from password protected PDFs. IMPORTANT: textract modifies the pdf-text-extract layoutdefault so that, instead of layout: layout, it uses layout:raw. It is not suggested you modify this without understanding what trouble that might get you in. See this GH issuefor why textract overrides that library's default.
  • typeOverride: Used with fromUrl, if set, rather than using the content-typefrom the URL request, will use the provided typeOverride.
  • includeAltText: When extracting HTML, whether or not to include alttext with the extracted text. By default this is false.

To use this configuration at the command line, prefix each open with a --.

Ex: textract image.png --tesseract.lang=deu


Commmand Line

If textract is installed gloablly, via npm install -g textract, then the following command will write the extracted text to the console for a file on the file system.

$ textract pathToFile


Configuration flags can be passed into textract via the command line.

textract pathToFile --preserveLineBreaks false

Parameters like exec.maxBuffercan be passed as you'd expect.

textract pathToFile --exec.maxBuffer 500000

And multiple flags can be used together.

textract pathToFile --preserveLineBreaks false --exec.maxBuffer 500000



var textract = require('textract');


There are several ways to extract text. For all methods, the extracted text and an error object are passed to a callback.

errorwill contain informative text about why the extraction failed. If textract does not currently extract files of the type provided, a typeNotFoundflag will be tossed on the error object.

textract.fromFileWithPath(filePath, function( error, text ) {})
textract.fromFileWithPath(filePath, config, function( error, text ) {})
File + mime type
textract.fromFileWithMimeAndPath(type, filePath, function( error, text ) {})
textract.fromFileWithMimeAndPath(type, filePath, config, function( error, text ) {})
Buffer + mime type
textract.fromBufferWithMime(type, buffer, function( error, text ) {})
textract.fromBufferWithMime(type, buffer, config, function( error, text ) {})
Buffer + file name/path
textract.fromBufferWithName(name, buffer, function( error, text ) {})
textract.fromBufferWithName(name, buffer, config, function( error, text ) {})

When passing a URL, the URL can either be a string, or a node.js URL object. Using the URL object allows fine grained control over the URL being used.

textract.fromUrl(url, function( error, text ) {})
textract.fromUrl(url, config, function( error, text ) {})

Testing Notes

Running Tests on a Mac?

  • sudo port install tesseract-chi-sim
  • sudo port install tesseract-eng
  • You will also want to disable textract's usage of textutil as the tests are based on output from antiword.
    • Go into /lib/extractors/{doc|doc-osx|rtf}and modify the code under if ( os.platform() === 'darwin' ) {. Uncommented the commented lines in these sections.







  • vedavaapi-textract

    vedavaapi textract vedavaapitextract CDN by jsDelivr A free, fast, and reliable Open S...

    4 个月前


扫码加入 JavaScript 社区