Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Tim Morgan http://timmorgan.org

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Find & Replace in Word Document with Ruby

I use this to open a "template" (really just a plain Word document with [text to replace] inside), do the substitutions, and save as a new filename.

   1  
   2  require 'win32ole'
   3  
   4  word = WIN32OLE.new('Word.Application')
   5  #word.Visible = true # uncomment if you want to see it happen
   6  doc = word.Documents.Open('c:\file_to_open.doc')
   7  {
   8    'name' => 'Tim Morgan',
   9    'date' => Date.today.strftime('%B %d, %Y'),
  10    ...
  11  }.each do |key, value|
  12    word.Selection.HomeKey(unit=6) # start at beginning
  13    find = word.Selection.Find
  14    find.Text = "[#{key}]" # text must be in square brackets
  15    while word.Selection.Find.Execute
  16      word.Selection.TypeText(text=value)
  17    end
  18  end
  19  doc.SaveAs('c:\output_file.doc')
  20  doc.Close

Script Word from Python

A quick and dirty class for working with Word via COM in Python. By far, not all of the power of Word is available here, but it's a good start for simple tasks.

   1  
   2  from win32com.client import constants, Dispatch
   3  
   4  import pythoncom
   5  
   6  wdStory = 6
   7  
   8  class WordDocument(object):
   9    """
  10    Some convenience methods for Word documents accessed
  11    through COM.
  12    """
  13    
  14    def __init__(self, visible=False):
  15      self.app = Dispatch("Word.Application")
  16      self.app.Visible = visible
  17    
  18    def new(self, filename=None):
  19      """
  20      Create a new Word document. If 'filename' specified,
  21      use the file as a template.
  22      """
  23      self.app.Documents.Add(filename)
  24    
  25    def open(self, filename):
  26      """
  27      Open an existing Word document for editing.
  28      """
  29      self.app.Documents.Open(filename)
  30    
  31    def save(self, filename=None):
  32      """
  33      Save the active document. If 'filename' is given,
  34      do a Save As.
  35      """
  36      if filename:
  37        self.app.ActiveDocument.SaveAs(filename)
  38      else:
  39        self.app.ActiveDocument.Save()
  40    
  41    def save_as(self, filename):
  42      return self.save(filename)
  43    
  44    def print_out(self):
  45      """
  46      Print the active document.
  47      """
  48      self.app.Application.PrintOut()
  49    
  50    def close(self):
  51      """
  52      Close the active document.
  53      """
  54      self.app.ActiveDocument.Close()
  55    
  56    def quit(self):
  57      """
  58      Quit Word.
  59      """
  60      return self.app.Quit()
  61    
  62    def find_and_replace(self, find_str, replace_str):
  63      """
  64      Find all occurances of 'find_str' and replace with 'replace_str'
  65      in the active document.
  66      """
  67      self.app.Selection.HomeKey(Unit=wdStory)
  68      find = self.app.Selection.Find
  69      find.Text = find_str
  70      while self.app.Selection.Find.Execute():
  71        self.app.Selection.TypeText(Text=replace_str)
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS