Coder Social home page Coder Social logo

docx_builder's Introduction

docx_builder

Programatically build Microsoft Word documents from ruby code, using the Office Open XML format.

Overview

docx_builder lets you create a new Word document from a template document. Then you can make method calls to add content to the end of the document, building it from the top down just as if you were writing it in Word.

Templates for docx_builder are just regular documents, not Word or Excel templates. If your template document contains text, this text will be in every new document that you create from it. The easiest way to create a new template document is to write it with Word, adjust the styling to your requirements, and then delete all the contents.

Details

Word documents are represented by Word::WordDocument objects. The following methods (there are others) are available to work with Word documents:

.blank_document(*options={}*) - Create a new document. If you supply a :base_documentoption then the new document is based on the document you supply. Otherwise it is initialized from an internal blank document.

.add_heading(*text*) - Add the specified text using the Heading1 style. Returns the paragraph.

.add_sub_heading(*text*) - Add the specified text using the Heading2 style. Returns the paragraph.

.add_paragraph(*text*, *options={}*) - Adds the specified text as a new paragraph. If you supply a :style option sets the paragraph style of the text accordingly. Returns the paragraph.

The text parameter allows specifying multiple runs with character styles. It can be:

  • A single string such as "text" - inserts that text
  • A hash such as {:content => "text", :style => "style"} - inserts that text with that character style
  • An array such as ["text1", "text2"] - inserts each piece of text as a separate run
  • An array of hashes such as [{:content => "text1", :style => "style1"}, {:content => "text2", :style => "style2"}] - inserts each piece of text as a separate run with the specified character style

.add_image(*image*, *options={}*) - Inserts the supplied Magick::Image or ImageList object as a new image. If you supply a :style option sets the style of the text accordingly.

.add_table(*hash*, *options={}*) - Inserts a table. By default, the keys of the hash provide the first row of data (column headings) in the table, and the values provide successive rows. If you supply a :table_style option it will be applied to the created table. If you supply a :column_widths option (as an array of twip measurements, 1440 twips = 1 inch) then the columns will be supplied accordingly. If you set the :skip_header option to true then the keys will not be rendered as a table row. If you supply a :column_styles option (as an array of strings) then the styles will be applied to the corresponding columns in the table.

.replace_all(*source_text*, *replacement*) - Searches and replaces in the document.

Acknowledgements

docx_builder is based on office_docs by mwelham. office_docs supports creation of both Word and Excel documents, and has been developed in a slightly different direction.

This work was supported by Labrador Omnimedia.

docx_builder's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.