Word document accessing using python

Reedick, Andrew jr9445 at ATT.COM
Wed Feb 13 10:37:47 EST 2008


> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Hari
> Sent: Wednesday, February 13, 2008 8:40 AM
> To: python-list at python.org
> Subject: Word document accessing using python
> 
> Hello,
> I want to fetch some data from the work document and to fill it inside
> excel sheet. For this I want to perform opening word documents and do
> some string manipulations for extracting data and to fill it inside
> excel sheet.
> Can any one help in this by saying how to do this or by giving some
> link where I can find some hints.
> 


Google for sample scripts.  Check the python documentation about
"Quick-Starts to Python and COM' and makepy.py.


Word Object Model Reference:
http://msdn2.microsoft.com/en-us/library/bb244515.aspx

import win32com.client
word = win32com.client.Dispatch("Word.Application")
word.Visible = True 
word.Documents.Open('c:\\some\\where\\foo.doc')

doc = word.Documents(1)
tables = doc.Tables
for table in tables:
	for row in table.Rows:
		for cell in row.Cells:
...


Excel reference:  http://msdn2.microsoft.com/en-us/library/bb149081.aspx

import win32com.client
import os
excel = win32com.client.Dispatch("Excel.Application", "Quit")
excel.Visible = 1

dir = os.getcwd()
book = excel.Workbooks.Open(dir + "/test.xls")
sheet = book.Worksheets(1)

for i in sheet.Range("A8:B9"):
	print i
print("active chart = " + str(excel.ActiveChart))
print("active sheet= " + str(excel.ActiveSheet))
print("\t" + str(excel.ActiveSheet.Name))
print("active workbook = " + str(excel.ActiveWorkbook))
print("\t" + str(excel.ActiveWorkbook.Name))


new_sheet = excel.Sheets.Add(None, None, None,
win32com.client.constants.xlWorksheet)
new_sheet.Name = "foo"

## import from a csv file
query_results = new_sheet.QueryTables.Add("TEXT;" + dir + "\\data.csv",
new_sheet.Cells(1,1))
query_results.TextFileParseType = win32com.client.constants.xlDelimited;
query_results.TextFileCommaDelimiter = 1;
query_results.Refresh();	



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA623





More information about the Python-list mailing list