-2

I'm trying to automate copying specific content from one word document(table) to other document(tables(s)), Please see below image to understand what I'm trying to do. Please suggest on how to make it work. I'm pretty comfortable with using python, if that's possible or any another tool. Thanks in advance.

IMAGE

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
sai
  • 11
  • 1
  • 1
  • 3

1 Answers1

1

If you wish to use python then you can use the win32com library (see Chun, Wesley J. Core Python Applications Programming (Core Series) Chapter 7).

import win32com.client as win32

and subsequently

word = win32.gencache.EnsureDispatch('Word.Application')
my_doc=word.Documents.Open(<path and document name>)
my_doc.Visible=True ' leave out if you don't need to see the documents.

if you are using a suitable IDE you will have intellisense for the Word object library

For word Table objects you can access text in cells using the following

my_doc.Tables(n).Range.Cell(x,y).range.text

Where

n is an integer representing the sequential number of the table in the range 1 to my_doc.Tables.Count

x,y are the row, column coordinates of the cell in the table.

If the tables are non uniform then you will have to use the cells(m) syntax where m is in the range 1 to my_doc.Tables.Range.Cells.Count.

To copy from one table to another use

my_doc2.Tables(n2).range.cell(x2,y2).range.text=my_doc1.Tables(n1).range.cell(x1,y1).range.text

or

my_doc2.Tables(n2).range.cells(m2).range.text=my_doc1.Tables(n1).range.cells(m1).range.text

(of course for the above m1 and m2 will be different offsets)

Also, as you show that there apppear to be three desinations you will need target Tables of n2_1, n2_2, n2_3 as appropriate.

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • Thank you for your clear explanation. I will update- once I work on this solution. Thanks again. – sai Sep 21 '18 at 14:01
  • It's working fine. Only change would be to use y_doc2.Tables(n2).Cell(x2,y2).Range.Text=my_doc1.Tables(n1).Cell(x1,y1).Range.Text. Thanks! – sai Oct 02 '18 at 19:44