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

2
  • Possible duplicate of Reading/Writing MS word files in python. Commented Nov 24, 2013 at 0: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? Commented Nov 24, 2013 at 0:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Why do you think 'animal data.txt' is an invalid path? As a filename it works perfectly.
@RolandSmith - Oh, I see. I misunderstood. I thought he was trying to use animal as an executable or a directory. Sorry. :*)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.