Home

Awesome

officegen

Creating Office Open XML files (Word, Excel and Powerpoint) for Microsoft Office 2007 and later without external tools, just pure Javascript. officegen should work on any environment that supports Node.js including Linux, OSX and Windows. officegen also supporting PowerPoint native charts objects with embedded data.

npm version dependencies devDependencies Build Status Join the chat at https://gitter.im/officegen/Lobby Backers on Open Collective Sponsors on Open Collective

Officegen logo Microsoft Office logo

Contributors:

This project exists thanks to all the people who contribute.

<a name="getstart"></a>

Getting Started:

Microsoft Powerpoint logo Microsoft Word logo Microsoft Excel logo

Officegen features overview:

<a name="install"></a>

Installation:

$ npm install officegen

<a name="getspptx"></a>

Microsoft PowerPoint basic usage example:

const officegen = require('officegen')
const fs = require('fs')

// Create an empty PowerPoint object:
let pptx = officegen('pptx')

// Let's add a title slide:

let slide = pptx.makeTitleSlide('Officegen', 'Example to a PowerPoint document')

// Pie chart slide example:

slide = pptx.makeNewSlide()
slide.name = 'Pie Chart slide'
slide.back = 'ffff00'
slide.addChart(
  {
    title: 'My production',
    renderType: 'pie',
    data:
	[
      {
        name: 'Oil',
        labels: ['Czech Republic', 'Ireland', 'Germany', 'Australia', 'Austria', 'UK', 'Belgium'],
        values: [301, 201, 165, 139, 128,  99, 60],
        colors: ['ff0000', '00ff00', '0000ff', 'ffff00', 'ff00ff', '00ffff', '000000']
      }
    ]
  }
)

// Let's generate the PowerPoint document into a file:

return new Promise((resolve, reject) => {
  let out = fs.createWriteStream('example.pptx')

  // This one catch only the officegen errors:
  pptx.on('error', function(err) {
    reject(err)
  })

  // Catch fs errors:
  out.on('error', function(err) {
    reject(err)
  })

  // End event after creating the PowerPoint file:
  out.on('close', function() {
    resolve()
  })

  // This async method is working like a pipe - it'll generate the pptx data and put it into the output stream:
  pptx.generate(out)
})

Since that officegen is using node.js events you can also create a document directly into a http respons stream:

const officegen = require('officegen')
const http = require('http')

/**
 * This is a simple web server that response with a PowerPoint document.
 */
http.createServer(function(req, res) {
  // We'll send a generated on the fly PowerPoint document without using files:
  if (req.url == '/') {
    // Create an empty PowerPoint object:
    let pptx = officegen('pptx')

    // Let's create a new slide:
    var slide = pptx.makeNewSlide()

    slide.name = 'Hello World'

    // Change the background color:
    slide.back = '000000'

    // Declare the default color to use on this slide:
    slide.color = 'ffffff'

    // Basic way to add text string:
    slide.addText('Created on the fly using a http server!')

    //
    // Let's generate the PowerPoint document directly into the response stream:
    //

    response.writeHead(200, {
      'Content-Type':
        'application/vnd.openxmlformats-officedocument.presentationml.presentation',
      'Content-disposition': 'attachment filename=out.pptx'
    })

	// Content types related to Office documents:
    // .xlsx   application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
    // .xltx   application/vnd.openxmlformats-officedocument.spreadsheetml.template
    // .potx   application/vnd.openxmlformats-officedocument.presentationml.template
    // .ppsx   application/vnd.openxmlformats-officedocument.presentationml.slideshow
    // .pptx   application/vnd.openxmlformats-officedocument.presentationml.presentation
    // .sldx   application/vnd.openxmlformats-officedocument.presentationml.slide
    // .docx   application/vnd.openxmlformats-officedocument.wordprocessingml.document
    // .dotx   application/vnd.openxmlformats-officedocument.wordprocessingml.template
    // .xlam   application/vnd.ms-excel.addin.macroEnabled.12
    // .xlsb   application/vnd.ms-excel.sheet.binary.macroEnabled.12

    // This one catch only the officegen errors:
    pptx.on('error', function(err) {
      res.end(err)
    })

    // Catch response errors:
    res.on('error', function(err) {
      res.end(err)
    })

    // End event after sending the PowerPoint data:
    res.on('finish', function() {
      res.end()
    })

    // This async method is working like a pipe - it'll generate the pptx data and pass it directly into the output stream:
    pptx.generate(res)
  } else {
    res.end('Invalid Request!')
  } // Endif.
}).listen(3000)

Where to go from here?

<a name="getspptx"></a>

Microsoft Word basic usage example:

const officegen = require('officegen')
const fs = require('fs')

// Create an empty Word object:
let docx = officegen('docx')

// Officegen calling this function after finishing to generate the docx document:
docx.on('finalize', function(written) {
  console.log(
    'Finish to create a Microsoft Word document.'
  )
})

// Officegen calling this function to report errors:
docx.on('error', function(err) {
  console.log(err)
})

// Create a new paragraph:
let pObj = docx.createP()

pObj.addText('Simple')
pObj.addText(' with color', { color: '000088' })
pObj.addText(' and back color.', { color: '00ffff', back: '000088' })

pObj = docx.createP()

pObj.addText('Since ')
pObj.addText('officegen 0.2.12', {
  back: '00ffff',
  shdType: 'pct12',
  shdColor: 'ff0000'
}) // Use pattern in the background.
pObj.addText(' you can do ')
pObj.addText('more cool ', { highlight: true }) // Highlight!
pObj.addText('stuff!', { highlight: 'darkGreen' }) // Different highlight color.

pObj = docx.createP()

pObj.addText('Even add ')
pObj.addText('external link', { link: 'https://github.com' })
pObj.addText('!')

pObj = docx.createP()

pObj.addText('Bold + underline', { bold: true, underline: true })

pObj = docx.createP({ align: 'center' })

pObj.addText('Center this text', {
  border: 'dotted',
  borderSize: 12,
  borderColor: '88CCFF'
})

pObj = docx.createP()
pObj.options.align = 'right'

pObj.addText('Align this text to the right.')

pObj = docx.createP()

pObj.addText('Those two lines are in the same paragraph,')
pObj.addLineBreak()
pObj.addText('but they are separated by a line break.')

docx.putPageBreak()

pObj = docx.createP()

pObj.addText('Fonts face only.', { font_face: 'Arial' })
pObj.addText(' Fonts face and size.', { font_face: 'Arial', font_size: 40 })

docx.putPageBreak()

pObj = docx.createP()

// We can even add images:
pObj.addImage('some-image.png')

// Let's generate the Word document into a file:

let out = fs.createWriteStream('example.docx')

out.on('error', function(err) {
  console.log(err)
})

// Async call to generate the output file:
docx.generate(out)

Where to go from here?

<a name="getspptx"></a>

Microsoft Excel basic usage example:

const officegen = require('officegen')
const fs = require('fs')

// Create an empty Excel object:
let xlsx = officegen('xlsx')

// Officegen calling this function after finishing to generate the xlsx document:
xlsx.on('finalize', function(written) {
  console.log(
    'Finish to create a Microsoft Excel document.'
  )
})

// Officegen calling this function to report errors:
xlsx.on('error', function(err) {
  console.log(err)
})

let sheet = xlsx.makeNewSheet()
sheet.name = 'Officegen Excel'

// Add data using setCell:

sheet.setCell('E7', 42)
sheet.setCell('I1', -3)
sheet.setCell('I2', 3.141592653589)
sheet.setCell('G102', 'Hello World!')

// The direct option - two-dimensional array:

sheet.data[0] = []
sheet.data[0][0] = 1
sheet.data[1] = []
sheet.data[1][3] = 'some'
sheet.data[1][4] = 'data'
sheet.data[1][5] = 'goes'
sheet.data[1][6] = 'here'
sheet.data[2] = []
sheet.data[2][5] = 'more text'
sheet.data[2][6] = 900
sheet.data[6] = []
sheet.data[6][2] = 1972

// Let's generate the Excel document into a file:

let out = fs.createWriteStream('example.xlsx')

out.on('error', function(err) {
  console.log(err)
})

// Async call to generate the output file:
xlsx.generate(out)

Where to go from here?

<a name="support"></a>

Support:

<a name="examples"></a>

Examples:

The official officegen Google Group:

officegen Google Group

The officegen Slack team:

Slack

Plans for the next release:

Trello

<a name="code"></a>

:coffee: The source code:

The project structure:

Code documentations:

To create the jsdoc documentation:

grunt jsdoc

External dependencies:

This project is using the following awesome libraries/utilities/services:

How to add new features:

The easiest way to add new features is by using the officegen internal plugins system.

<a name="credits"></a>

Credit:

<a name="contributors"></a>

Contributors:

This project exists thanks to all the people who contribute.

<a href="https://github.com/Ziv-Barber/officegen/graphs/contributors"><img src="https://opencollective.com/officegen/contributors.svg?width=890&button=false" /></a>

<a name="backers"></a>

Backers:

Thank you to all our backers! 🙏 [Become a backer]

<a href="https://opencollective.com/officegen#backers" target="_blank"><img src="https://opencollective.com/officegen/backers.svg?width=890"></a>

<a name="sponsors"></a>

Sponsors:

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

<a href="https://opencollective.com/officegen/sponsor/0/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/0/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/1/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/1/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/2/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/2/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/3/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/3/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/4/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/4/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/5/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/5/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/6/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/6/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/7/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/7/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/8/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/8/avatar.svg"></a> <a href="https://opencollective.com/officegen/sponsor/9/website" target="_blank"><img src="https://opencollective.com/officegen/sponsor/9/avatar.svg"></a>