<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: com code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 13:10:35 GMT</pubDate>
    <description>DZone Snippets: com code</description>
    <item>
      <title>Find &amp; Replace in Word Document with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4747</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'win32ole'&lt;br /&gt;&lt;br /&gt;word = WIN32OLE.new('Word.Application')&lt;br /&gt;#word.Visible = true # uncomment if you want to see it happen&lt;br /&gt;doc = word.Documents.Open('c:\file_to_open.doc')&lt;br /&gt;{&lt;br /&gt;  'name' =&gt; 'Tim Morgan',&lt;br /&gt;  'date' =&gt; Date.today.strftime('%B %d, %Y'),&lt;br /&gt;  ...&lt;br /&gt;}.each do |key, value|&lt;br /&gt;  word.Selection.HomeKey(unit=6) # start at beginning&lt;br /&gt;  find = word.Selection.Find&lt;br /&gt;  find.Text = "[#{key}]" # text must be in square brackets&lt;br /&gt;  while word.Selection.Find.Execute&lt;br /&gt;    word.Selection.TypeText(text=value)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;doc.SaveAs('c:\output_file.doc')&lt;br /&gt;doc.Close&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 12 Nov 2007 14:48:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4747</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Script Word from Python</title>
      <link>http://snippets.dzone.com/posts/show/2037</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from win32com.client import constants, Dispatch&lt;br /&gt;&lt;br /&gt;import pythoncom&lt;br /&gt;&lt;br /&gt;wdStory = 6&lt;br /&gt;&lt;br /&gt;class WordDocument(object):&lt;br /&gt;  """&lt;br /&gt;  Some convenience methods for Word documents accessed&lt;br /&gt;  through COM.&lt;br /&gt;  """&lt;br /&gt;  &lt;br /&gt;  def __init__(self, visible=False):&lt;br /&gt;    self.app = Dispatch("Word.Application")&lt;br /&gt;    self.app.Visible = visible&lt;br /&gt;  &lt;br /&gt;  def new(self, filename=None):&lt;br /&gt;    """&lt;br /&gt;    Create a new Word document. If 'filename' specified,&lt;br /&gt;    use the file as a template.&lt;br /&gt;    """&lt;br /&gt;    self.app.Documents.Add(filename)&lt;br /&gt;  &lt;br /&gt;  def open(self, filename):&lt;br /&gt;    """&lt;br /&gt;    Open an existing Word document for editing.&lt;br /&gt;    """&lt;br /&gt;    self.app.Documents.Open(filename)&lt;br /&gt;  &lt;br /&gt;  def save(self, filename=None):&lt;br /&gt;    """&lt;br /&gt;    Save the active document. If 'filename' is given,&lt;br /&gt;    do a Save As.&lt;br /&gt;    """&lt;br /&gt;    if filename:&lt;br /&gt;      self.app.ActiveDocument.SaveAs(filename)&lt;br /&gt;    else:&lt;br /&gt;      self.app.ActiveDocument.Save()&lt;br /&gt;  &lt;br /&gt;  def save_as(self, filename):&lt;br /&gt;    return self.save(filename)&lt;br /&gt;  &lt;br /&gt;  def print_out(self):&lt;br /&gt;    """&lt;br /&gt;    Print the active document.&lt;br /&gt;    """&lt;br /&gt;    self.app.Application.PrintOut()&lt;br /&gt;  &lt;br /&gt;  def close(self):&lt;br /&gt;    """&lt;br /&gt;    Close the active document.&lt;br /&gt;    """&lt;br /&gt;    self.app.ActiveDocument.Close()&lt;br /&gt;  &lt;br /&gt;  def quit(self):&lt;br /&gt;    """&lt;br /&gt;    Quit Word.&lt;br /&gt;    """&lt;br /&gt;    return self.app.Quit()&lt;br /&gt;  &lt;br /&gt;  def find_and_replace(self, find_str, replace_str):&lt;br /&gt;    """&lt;br /&gt;    Find all occurances of 'find_str' and replace with 'replace_str'&lt;br /&gt;    in the active document.&lt;br /&gt;    """&lt;br /&gt;    self.app.Selection.HomeKey(Unit=wdStory)&lt;br /&gt;    find = self.app.Selection.Find&lt;br /&gt;    find.Text = find_str&lt;br /&gt;    while self.app.Selection.Find.Execute():&lt;br /&gt;      self.app.Selection.TypeText(Text=replace_str)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 00:10:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2037</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Script Excel from Python</title>
      <link>http://snippets.dzone.com/posts/show/2036</link>
      <description>A quick and dirty class for working with Excel via COM in Python. By far, not all of the power of Excel is available here, but it's a good start for simple tasks.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;from win32com.client import constants, Dispatch&lt;br /&gt;import pythoncom&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;borderTop = 3&lt;br /&gt;borderBottom = 4&lt;br /&gt;borderLeft = 1&lt;br /&gt;borderRight = 2&lt;br /&gt;borderSolid = 1&lt;br /&gt;borderDashed = 2&lt;br /&gt;borderDotted = 3&lt;br /&gt;colorBlack = 1&lt;br /&gt;directionUp = -4162&lt;br /&gt;directionDown = -4121&lt;br /&gt;directionLeft = -4131&lt;br /&gt;directionRight = -4152&lt;br /&gt;&lt;br /&gt;class ExcelDocument(object):&lt;br /&gt;  """&lt;br /&gt;  Some convenience methods for Excel documents accessed&lt;br /&gt;  through COM.&lt;br /&gt;  """&lt;br /&gt;  &lt;br /&gt;  def __init__(self, visible=False):&lt;br /&gt;    self.app = Dispatch("Excel.Application")&lt;br /&gt;    self.app.Visible = visible&lt;br /&gt;    self.sheet = 1&lt;br /&gt;  &lt;br /&gt;  def new(self, filename=None):&lt;br /&gt;    """&lt;br /&gt;    Create a new Excel workbook. If 'filename' specified,&lt;br /&gt;    use the file as a template.&lt;br /&gt;    """&lt;br /&gt;    self.app.Workbooks.Add(filename)&lt;br /&gt;  &lt;br /&gt;  def open(self, filename):&lt;br /&gt;    """&lt;br /&gt;    Open an existing Excel workbook for editing.&lt;br /&gt;    """&lt;br /&gt;    self.app.Workbooks.Open(filename)&lt;br /&gt;  &lt;br /&gt;  def set_sheet(self, sheet):&lt;br /&gt;    """&lt;br /&gt;    Set the active worksheet.&lt;br /&gt;    """&lt;br /&gt;    self.sheet = sheet&lt;br /&gt;  &lt;br /&gt;  def get_range(self, range):&lt;br /&gt;    """&lt;br /&gt;    Get a range object for the specified range or single cell.&lt;br /&gt;    """&lt;br /&gt;    return self.app.ActiveWorkbook.Sheets(self.sheet).Range(range)&lt;br /&gt;  &lt;br /&gt;  def set_value(self, cell, value=''):&lt;br /&gt;    """&lt;br /&gt;    Set the value of 'cell' to 'value'.&lt;br /&gt;    """&lt;br /&gt;    self.get_range(cell).Value = value&lt;br /&gt;  &lt;br /&gt;  def get_value(self, cell):&lt;br /&gt;    """&lt;br /&gt;    Get the value of 'cell'.&lt;br /&gt;    """&lt;br /&gt;    value = self.get_range(cell).Value&lt;br /&gt;    if isinstance(value, tuple):&lt;br /&gt;      value = [v[0] for v in value]&lt;br /&gt;    return value&lt;br /&gt;  &lt;br /&gt;  def set_border(self, range, side, line_style=borderSolid, color=colorBlack):&lt;br /&gt;    """&lt;br /&gt;    Set a border on the specified range of cells or single cell.&lt;br /&gt;    'range' = range of cells or single cell&lt;br /&gt;    'side' = one of borderTop, borderBottom, borderLeft, borderRight&lt;br /&gt;    'line_style' = one of borderSolid, borderDashed, borderDotted, others?&lt;br /&gt;    'color' = one of colorBlack, others?&lt;br /&gt;    """&lt;br /&gt;    range = self.get_range(range).Borders(side)&lt;br /&gt;    range.LineStyle = line_style&lt;br /&gt;    range.Color = color&lt;br /&gt;  &lt;br /&gt;  def sort(self, range, key_cell):&lt;br /&gt;    """&lt;br /&gt;    Sort the specified 'range' of the activeworksheet by the&lt;br /&gt;    specified 'key_cell'.&lt;br /&gt;    """&lt;br /&gt;    range.Sort(Key1=self.get_range(key_cell), Order1=1, Header=0, OrderCustom=1, MatchCase=False, Orientation=1)&lt;br /&gt;  &lt;br /&gt;  def hide_row(self, row, hide=True):&lt;br /&gt;    """&lt;br /&gt;    Hide the specified 'row'.&lt;br /&gt;    Specify hide=False to show the row.&lt;br /&gt;    """&lt;br /&gt;    self.get_range('a%s' % row).EntireRow.Hidden = hide&lt;br /&gt;  &lt;br /&gt;  def hide_column(self, column, hide=True):&lt;br /&gt;    """&lt;br /&gt;    Hide the specified 'column'.&lt;br /&gt;    Specify hide=False to show the column.&lt;br /&gt;    """&lt;br /&gt;    self.get_range('%s1' % column).EntireColumn.Hidden = hide&lt;br /&gt;  &lt;br /&gt;  def delete_row(self, row, shift=directionUp):&lt;br /&gt;    """&lt;br /&gt;    Delete the entire 'row'.&lt;br /&gt;    """&lt;br /&gt;    self.get_range('a%s' % row).EntireRow.Delete(Shift=shift)&lt;br /&gt;  &lt;br /&gt;  def delete_column(self, column, shift=directionLeft):&lt;br /&gt;    """&lt;br /&gt;    Delete the entire 'column'.&lt;br /&gt;    """&lt;br /&gt;    self.get_range('%s1' % column).EntireColumn.Delete(Shift=shift)&lt;br /&gt;    &lt;br /&gt;  def fit_column(self, column):&lt;br /&gt;    """&lt;br /&gt;    Resize the specified 'column' to fit all its contents.&lt;br /&gt;    """&lt;br /&gt;    self.get_range('%s1' % column).EntireColumn.AutoFit()&lt;br /&gt;  &lt;br /&gt;  def save(self):&lt;br /&gt;    """&lt;br /&gt;    Save the active workbook.&lt;br /&gt;    """&lt;br /&gt;    self.app.ActiveWorkbook.Save()&lt;br /&gt;  &lt;br /&gt;  def save_as(self, filename, delete_existing=False):&lt;br /&gt;    """&lt;br /&gt;    Save the active workbook as a different filename.&lt;br /&gt;    If 'delete_existing' is specified and the file already&lt;br /&gt;    exists, it will be deleted before saving.&lt;br /&gt;    """&lt;br /&gt;    if delete_existing and os.path.exists(filename):&lt;br /&gt;      os.remove(filename)&lt;br /&gt;    self.app.ActiveWorkbook.SaveAs(filename)&lt;br /&gt;  &lt;br /&gt;  def print_out(self):&lt;br /&gt;    """&lt;br /&gt;    Print the active workbook.&lt;br /&gt;    """&lt;br /&gt;    self.app.Application.PrintOut()&lt;br /&gt;  &lt;br /&gt;  def close(self):&lt;br /&gt;    """&lt;br /&gt;    Close the active workbook.&lt;br /&gt;    """&lt;br /&gt;    self.app.ActiveWorkbook.Close()&lt;br /&gt;  &lt;br /&gt;  def quit(self):&lt;br /&gt;    """&lt;br /&gt;    Quit Excel.&lt;br /&gt;    """&lt;br /&gt;    return self.app.Quit()&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 16 May 2006 00:07:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2036</guid>
      <author>timmorgan (Tim Morgan)</author>
    </item>
    <item>
      <title>Mass conversion from word to HTML</title>
      <link>http://snippets.dzone.com/posts/show/416</link>
      <description>I got 1000 word files. Each contains 1 main image and some decorations.&lt;br /&gt;(This is actually a big book scanned into 1-file-per-page format)&lt;br /&gt;I need to extract all the images. What do I do?&lt;br /&gt;&lt;br /&gt;Python can do some automation using COM. (or something like that)&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import pythoncom, win32com.client&lt;br /&gt;&lt;br /&gt;app = win32com.client.gencache.EnsureDispatch("Word.Application")&lt;br /&gt;&lt;br /&gt;doc = 'C:\\lang\\try\\bdham\\p1'&lt;br /&gt;app.Documents.Open(doc + '.doc')&lt;br /&gt;app.ActiveDocument.SaveAs(doc + '.html', FileFormat=win32com.client.constants.wdFormatHTML)&lt;br /&gt;app.ActiveDocument.Close()&lt;br /&gt;# now repeat with p2, p3, etc.&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Actually, I should put it in a loop. But this non-loop version&lt;br /&gt;is easier to read and remember.</description>
      <pubDate>Tue, 28 Jun 2005 00:02:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/416</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
