[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Word document accessing using python

Hari

2/13/2008 1:40:00 PM

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.

Thanks in advance,
Hari
5 Answers

Juan_Pablo

2/13/2008 2:15:00 PM

0

On 13 feb, 10:40, Hari <p.r.hari...@gmail.com> wrote:
> 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.
>
> Thanks in advance,
> Hari


I have a similar problem
look this
http://www.thescripts.com/forum/thread...
http://www.velocityreviews.com/forums/t330073-opening-ms-word-files-via-p...

I hope you can supplement with more information

Reedick, Andrew

2/13/2008 3:38:00 PM

0

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Hari
> Sent: Wednesday, February 13, 2008 8:40 AM
> To: python-list@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/bb2...

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/bb1...

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


Juan_Pablo

2/13/2008 6:07:00 PM

0


> import win32com.client

but, window32com.client is only functional in windows

Aaron Brady

2/13/2008 10:19:00 PM

0

On Feb 13, 12:07 pm, Juan_Pablo <jabar...@gmail.com> wrote:
> > import win32com.client
>
>  but, window32com.client is only functional in  windows

Excel can read XML.

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell><Data ss:Type="String">abc</Data></Cell>
<Cell><Data ss:Type="Number">123</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>

Word command line to save as text, or the reader from Microsoft?

Reedick, Andrew

2/13/2008 10:50:00 PM

0

> -----Original Message-----
> From: python-list-bounces+jr9445=att.com@python.org [mailto:python-
> list-bounces+jr9445=att.com@python.org] On Behalf Of Juan_Pablo
> Sent: Wednesday, February 13, 2008 1:07 PM
> To: python-list@python.org
> Subject: Re: Word document accessing using python
>
>
> > import win32com.client
>
> but, window32com.client is only functional in windows


Correct. Microsoft tries very hard to make sure that its applications
and their saved data are only readable/usable/automate-able using
MS-Office and MS-Windows (and preferably the current versions of each.)
If you can't create a quality product that people will willingly pay
money for, then use lock-in to squeeze money out of them. =/

As suggested by castironpi, you could save the word docs in a neutral
format such as text or xml. Another idea is to find a module or
commercial product that will read word files. In theory, from what I've
heard, Open Office can read some (most?) word formats, and since OO has
a scriptable api (does it?), you could use OO to read the word doc and
use OO's api to get the relevant data. You would need to test it out.



*****

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. GA621