officegen Documentation

repository·master·Indexed 25 days ago

https://github.com/ziv-barber/officegen

A pure JavaScript library for creating Microsoft Office Open XML files (Word .docx, PowerPoint .pptx/.ppsx, and Excel .xlsx) for Office 2007 and later. It uses Node.js streams and works in any Node.js environment without requiring external command-line tools. The library supports document configuration, text formatting, table creation, image insertion, and extensibility through a plugin system for adding new document types or features.

Tokens
11.8K
Snippets
14
Records
63
Agent score
83%

What's inside officegen

  1. Project Architecture and Internal Modules

    master

    The officegen project is structured around a core engine and specific document generators implemented as plugins.

    • office/index.js: The main entry point.
    • office/lib/basicgen.js: The generic engine used to build various document types. All document generators must use this plugins API.
    • office/lib/docplug.js: An optional engine used to create plugin APIs for document generators.
    • office/lib/msofficegen.js: A template plugin that provides common Microsoft Office functionality used by all Microsoft-based generators.

    Document generators available in the library include:

    • genpptx.js: Creates PPTX/PPSX documents.
    • genxlsx.js: Creates XLSX documents.
    • gendocx.js: Creates DOCX documents.
  2. Add new features using the plugins system

    master
    To extend the functionality of officegen, the recommended approach is to use the internal plugins system. Detailed documentation for this can be found in the manual/advanced/plugins/README.md file within the repository.
  3. Register a new document type plugin

    master

    You can extend officegen by registering new document types using the baseobj.plugins.registerDocType method. This allows you to implement support for new formats (like OpenOffice or custom types) by providing a factory function that extends the base generator object.

    To register a type, call registerDocType with the following arguments:

    • type_code (string): The unique identifier for the new type.
    • factory_function (function): A function that receives genobj, new_type, options, gen_private, and type_info to implement the document logic.
    • options (object): Configuration options for the new type.
    • base_type (constant): The base document category (e.g., baseobj.docType.PRESENTATION).
    • display_name (string): The human-readable name of the document type.
    var baseobj = require('./basicgen.js')
    
    /**
     * Extend officegen object with some new document type support.
     *
     * @param {object} genobj The object to extend.
     * @param {string} new_type The type of object to create.
     * @param {object} options The object's options.
     * @param {object} gen_private Access to the internals of this object.
     * @param {object} type_info Additional information about this type.
     */
    function makeSomeType(genobj, new_type, options, gen_private, type_info) {
      // ...
    }
    
    baseobj.plugins.registerDocType(
      'mytype', // The type code string.
      makeSomeType,
      {},
      baseobj.docType.PRESENTATION,
      'My Document'
    )
  4. Extend officegen with new document types

    master

    You can extend officegen by implementing new document type plugins. There are two primary categories of document type extensions:

    1. Microsoft Office based document types: For creating files compatible with Microsoft Office formats.
    2. OpenOffice based document types: For creating files compatible with OpenOffice/ODF formats.
  5. Extend existing document types with new features

    master

    If you want to add new features to existing document types rather than creating a new format, you can use the following plugin approaches:

    • docplug: A general plugin mechanism for extending document types.
    • Microsoft PowerPoint (pptx): Specifically for adding features to the PowerPoint document type.
    • Microsoft Word (docx): Specifically for adding features to the Word document type.
    • Microsoft Excel (xlsx): Specifically for adding features to the Excel document type.
  6. Generate Microsoft Excel (.xlsx) documents

    master

    Use officegen('xlsx') to create an Excel object. You can create new sheets using makeNewSheet(). Data can be added to specific cells using setCell(cell, value) or by populating the sheet.data two-dimensional array directly. The generate() method streams the output to a writable stream.

    const officegen = require('officegen')
    const fs = require('fs')
    
    // Create an empty Excel object:
    let xlsx = officegen('xlsx')
    
    let sheet = xlsx.makeNewSheet()
    sheet.name = 'Officegen Excel'
    
    // Add data using setCell:
    sheet.setCell('E7', 42)
    sheet.setCell('I1', -3)
    
    // Add data using a two-dimensional array:
    sheet.data[0] = []
    sheet.data[0][0] = 1
    sheet.data[1] = []
    sheet.data[1][3] = 'some'
    
    // Generate the document to a file:
    let out = fs.createWriteStream('example.xlsx')
    xlsx.generate(out)
  7. Generate Microsoft Word (.docx) documents

    master

    Use officegen('docx') to create a Word object. You can create paragraphs using createP(), add text with specific styles (color, font, alignment, bold, underline, highlight), add images, and insert page breaks. The generate() method streams the output to a writable stream.

    const officegen = require('officegen')
    const fs = require('fs')
    
    // Create an empty Word object:
    let docx = officegen('docx')
    
    // Create a new paragraph with styled text:
    let pObj = docx.createP()
    pObj.addText('Simple')
    pObj.addText(' with color', { color: '000088' })
    pObj.addText(' and back color.', { color: '00ffff', back: '000088' })
    
    // Add a paragraph with a hyperlink:
    pObj = docx.createP()
    pObj.addText('Even add ', { bold: true })
    pObj.addText('external link', { link: 'https://github.com' })
    
    // Add a page break:
    docx.putPageBreak()
    
    // Add an image:
    pObj = docx.createP()
    pObj.addImage('some-image.png')
    
    // Generate the document to a file:
    let out = fs.createWriteStream('example.docx')
    docx.generate(out)
  8. Generate Microsoft PowerPoint (.pptx) documents

    master

    Use officegen('pptx') to create a PowerPoint object. You can create title slides, new slides, add text, images, and native charts. The generate() method works like a pipe, allowing you to stream the output to a file system write stream or an HTTP response stream.

    const officegen = require('officegen')
    const fs = require('fs')
    
    // Create an empty PowerPoint object:
    let pptx = officegen('pptx')
    
    // Add a title slide:
    let slide = pptx.makeTitleSlide('Officegen', 'Example to a PowerPoint document')
    
    // Add a new slide with a chart:
    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']
          }
        ]
      }
    )
    
    // Generate the document to a file:
    let out = fs.createWriteStream('example.pptx')
    pptx.generate(out)
  9. Initialize a PowerPoint (pptx) document

    master

    To create a PowerPoint document, require officegen and call it with the type 'pptx'. You can provide an options object to configure document-level settings or a custom theme.

    To use a custom theme, provide the XML content from an existing Office document (typically found in ppt/theme/theme1.xml).