a trivial question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

a trivial question

cool_go_blue
I try to use comprehensions when I am learning Python. After opening a word document, i try to read the 2nd column of a table for each row. I print out the words as follows:
 

for row in range(1,len(self.doc.Tables(1).Rows)+1):

   for word in str(self.doc.Tables(1).Cell(row,2)).split():

      if word not in stopwords:

         print word
 
But I get a runtime error when I have the following code:
 

content = [[word for word in str(self.doc.Tables(1).Cell(row,2).split()) if word not in stopwords]

for row in range(1,len(self.doc.Tables(1).Rows)+1)]

 

The error is as follows:

 

Traceback (most recent call last):

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 111, in <module>

array.setup()

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 79, in setup

for row in range(1,len(self.doc.Tables(1).Rows)+1)

File "F:\Softwares\Working\Languages\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in __getattr__

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Word 14.0 Object Library.Cell instance at 0x51383312>' object has no attribute 'split'

 

what is wrong with my code? Thanks.

B.


_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|

Re: a trivial question

Paul_Koning

You have a misplaced parenthesis.  The working code has str (…Cell(…) ).split() and the failing code has str (…Cell(…).split() ) –in other words, the bad code has split() applied to the argument of str() rather than the result of str().

 

               paul

 

From: python-win32-bounces+pkoning=[hidden email] [mailto:python-win32-bounces+pkoning=[hidden email]] On Behalf Of cool_go_blue
Sent: Thursday, March 08, 2012 12:15 PM
To: [hidden email]
Subject: [python-win32] a trivial question

 

I try to use comprehensions when I am learning Python. After opening a word document, i try to read the 2nd column of a table for each row. I print out the words as follows:

 

for row in range(1,len(self.doc.Tables(1).Rows)+1):

   for word in str(self.doc.Tables(1).Cell(row,2)).split():

      if word not in stopwords:

         print word

 

But I get a runtime error when I have the following code:

 

content = [[word for word in str(self.doc.Tables(1).Cell(row,2).split()) if word not in stopwords]

for row in range(1,len(self.doc.Tables(1).Rows)+1)]

 

The error is as follows:

 

Traceback (most recent call last):

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 111, in <module>

array.setup()

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 79, in setup

for row in range(1,len(self.doc.Tables(1).Rows)+1)

File "F:\Softwares\Working\Languages\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in __getattr__

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Word 14.0 Object Library.Cell instance at 0x51383312>' object has no attribute 'split'

 

what is wrong with my code? Thanks.

B.

 


_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|

Re: a trivial question

Randy Syring-2
In reply to this post by cool_go_blue

On 03/08/2012 12:14 PM, cool_go_blue wrote:
[[word for word in str(self.doc.Tables(1).Cell(row,2).split()) if word not in stopwords]

You have a parenthesis in the wrong place.  You are doing .split() on the column object, not the result of str().  Change it to:

str(self.doc.Tables(1).Cell(row,2)).split()
---------------------------------------------
Randy Syring
Development & Executive Director
Level 12 Technologies (formerly Intelicom)
Direct: 502-276-0459
Office: 502-212-9913

Intelicom is now Level 12 Technologies, learn more about our name change.
Please update your address book with my new email address.

Principled People, Technology that Works


_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|

Re: a trivial question

Pham, Hien
In reply to this post by cool_go_blue

Try this:

 

tableSize = self.doc.getTableSize(tbl)

 

for row in range(tableSize[1]):

    for col in range(tableSize[0]):

        print procTbl.Cell(row+1, col+1).Range).Text

 

 

From: python-win32-bounces+hien.pham=[hidden email] [mailto:python-win32-bounces+hien.pham=[hidden email]] On Behalf Of cool_go_blue
Sent: Thursday, March 08, 2012 12:15 PM
To: [hidden email]
Subject: [python-win32] a trivial question

 

I try to use comprehensions when I am learning Python. After opening a word document, i try to read the 2nd column of a table for each row. I print out the words as follows:

 

for row in range(1,len(self.doc.Tables(1).Rows)+1):

   for word in str(self.doc.Tables(1).Cell(row,2)).split():

      if word not in stopwords:

         print word

 

But I get a runtime error when I have the following code:

 

content = [[word for word in str(self.doc.Tables(1).Cell(row,2).split()) if word not in stopwords]

for row in range(1,len(self.doc.Tables(1).Rows)+1)]

 

The error is as follows:

 

Traceback (most recent call last):

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 111, in <module>

array.setup()

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 79, in setup

for row in range(1,len(self.doc.Tables(1).Rows)+1)

File "F:\Softwares\Working\Languages\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in __getattr__

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Word 14.0 Object Library.Cell instance at 0x51383312>' object has no attribute 'split'

 

what is wrong with my code? Thanks.

B.

 


_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|

Re: a trivial question

Vernon D. Cole
In reply to this post by cool_go_blue
self.doc.Tables(1).Cell(row,2) is not a string, and therefore has no .split() method.
str(self.doc.Tables(1).Cell(row,2)) returns a string, so it does have a split() method and therefore
str(self.doc.Tables(1).Cell(row,2)).split() is correct, but
str(self.doc.Tables(1).Cell(row,2).split()) is not.  Note the different position on the double parens ))
--
Vernon

On Thu, Mar 8, 2012 at 10:14 AM, cool_go_blue <[hidden email]> wrote:
I try to use comprehensions when I am learning Python. After opening a word document, i try to read the 2nd column of a table for each row. I print out the words as follows:
 

for row in range(1,len(self.doc.Tables(1).Rows)+1):

   for word in str(self.doc.Tables(1).Cell(row,2)).split():

      if word not in stopwords:
         print word
 
But I get a runtime error when I have the following code:
 

content = [[word

for word in str(self.doc.Tables(1).Cell(row,2).split()) if word not in stopwords]

for row in range(1,len(self.doc.Tables(1).Rows)+1)]

 

The error is as follows:

 

Traceback (most recent call last):

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 111, in <module>

array.setup()

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 79, in setup

for row in range(1,len(self.doc.Tables(1).Rows)+1)

File "F:\Softwares\Working\Languages\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in __getattr__

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Word 14.0 Object Library.Cell instance at 0x51383312>' object has no attribute 'split'

 

what is wrong with my code? Thanks.

B.


_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32



_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32
Reply | Threaded
Open this post in threaded view
|

Re: a trivial question

cool_go_blue
Thanks for all resonses. I just concentrated on the "for row in ..." statement. Now I have another question. I would like to read a document with various structures such as title, subtitle, paragraph, table (as I did previously) and bullet etc. How can I get these contents for further analysis? Thanks.
B.

--- On Thu, 3/8/12, Vernon Cole <[hidden email]> wrote:

From: Vernon Cole <[hidden email]>
Subject: Re: [python-win32] a trivial question
To: "cool_go_blue" <[hidden email]>
Cc: [hidden email]
Date: Thursday, March 8, 2012, 2:48 PM

self.doc.Tables(1).Cell(row,2) is not a string, and therefore has no .split() method.
str(self.doc.Tables(1).Cell(row,2)) returns a string, so it does have a split() method and therefore
str(self.doc.Tables(1).Cell(row,2)).split() is correct, but
str(self.doc.Tables(1).Cell(row,2).split()) is not.  Note the different position on the double parens ))
--
Vernon

On Thu, Mar 8, 2012 at 10:14 AM, cool_go_blue <cool_go_blue@...> wrote:
I try to use comprehensions when I am learning Python. After opening a word document, i try to read the 2nd column of a table for each row. I print out the words as follows:
 

for row in range(1,len(self.doc.Tables(1).Rows)+1):

   for word in str(self.doc.Tables(1).Cell(row,2)).split():

      if word not in stopwords:

         print word
 
But I get a runtime error when I have the following code:
 

content = [[word for word in str(self.doc.Tables(1).Cell(row,2).split()) if word not in stopwords]

for row in range(1,len(self.doc.Tables(1).Rows)+1)]

 

The error is as follows:

 

Traceback (most recent call last):

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 111, in <module>

array.setup()

File "J:\MyProjects\Python\VectorSpaceTry\src\ReadCorpus.py", line 79, in setup

for row in range(1,len(self.doc.Tables(1).Rows)+1)

File "F:\Softwares\Working\Languages\Python27\lib\site-packages\win32com\client\__init__.py", line 465, in __getattr__

raise AttributeError("'%s' object has no attribute '%s'" % (repr(self), attr))

AttributeError: '<win32com.gen_py.Microsoft Word 14.0 Object Library.Cell instance at 0x51383312>' object has no attribute 'split'

 

what is wrong with my code? Thanks.

B.


_______________________________________________
python-win32 mailing list
python-win32@...
http://mail.python.org/mailman/listinfo/python-win32



_______________________________________________
python-win32 mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/python-win32