regular expression error ?

L

Lance Hoffmeyer

why isn't re.search recognizing "file"? I can print the contents of "file".


import win32com.client
import re

D=MSWord.Documents.Open('C:/test/Test.doc')
file=D.Content
print file

match = re.search('(Table ?)', file)
if match:
print "Table Number is: ", match.group(1)


Traceback (most recent call last):
File "test.py", line 21, in ?
match = re.search('(Table ?)', file)
File "C:\Python24\Lib\sre.py", line 134, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
 
B

Bruno Desthuilliers

Lance Hoffmeyer a écrit :
why isn't re.search recognizing "file"? I can print the contents of "file".


import win32com.client
import re

D=MSWord.Documents.Open('C:/test/Test.doc')
file=D.Content
print file

match = re.search('(Table ?)', file)
if match:
print "Table Number is: ", match.group(1)


Traceback (most recent call last):
File "test.py", line 21, in ?
match = re.search('(Table ?)', file)
File "C:\Python24\Lib\sre.py", line 134, in search
return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

The fact that you can print 'file' doesn't mean it's a string. You may
want to try this:

(...)
# let's see what it really is:
print type(D.Content), repr(D.Content)
# and try to make it usable for re:
file = str(D.Content)
(...)

NB : not tested, I don't have Word on my Linix box !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top