0

Can I import a word document into a python program so that its content can be read and questions can be answered using the data in the document. what would be procedure of using the data in the file

with open('animal data.txt', 'r') 

i used this but is not working

rggod
  • 611
  • 5
  • 11
  • 19
  • Possible duplicate of [Reading/Writing MS word files in python](http://stackoverflow.com/questions/188444/reading-writing-ms-word-files-in-python). – Roland Smith Nov 24 '13 at 00:11
  • Your question is a little bit odd to me. You ask about a word document, but your example uses a `.txt` file, not a `.doc` or `.docx` file. What exactly do you mean? – Pandu Nov 24 '13 at 00:13

2 Answers2

1

You need to assign the file object to a variable using as:

with open('animal data.txt', 'r') as myfile:
    ...

Now, myfile will be the file object and you can use it freely in the with-block.

  • Why do you think 'animal data.txt' is an invalid path? As a filename it works perfectly. – Roland Smith Nov 24 '13 at 00:23
  • @RolandSmith - Oh, I see. I misunderstood. I thought he was trying to use `animal` as an executable or a directory. Sorry. :*) –  Nov 24 '13 at 00:24
1

Extracting data from a MS Word document involves a lot more than just reading it!

For reading modern Word documents with the docx extension you can use python-docx. These documents are basically a bunch of XML files in a ZIP container.

Older doc files are basically undocumented binary blobs.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94