473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Opening a word file in Python [not quite solved]

Folks,

I have written a word frequency counter program in python that works well for .txt files; but it cannot handle .DOC files.
Can someone help me to resolve this issue???
Oct 23 '06 #1
4 3445
bartonc
6,596 Expert 4TB
Folks,

I have written a word frequency counter program in python that works well for .txt files; but it cannot handle .DOC files.
Can someone help me to resolve this issue???
If you can make a macro or write Visual Basic in order to get Word to do the work you need, then there is a way to get word to give you the info from PythonWin using the COM extentions. I haven't used COM yet, but can point you to some examples if this seem like the way you want to go with this.
Oct 23 '06 #2
fuffens
38
I thought that the following COM function calls would solve the problem, but it turns out that the Words.Count operation not just counts the number of words. It also counts the number of separators such as .,?

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import *
  2. word = Dispatch("Word.Application")
  3. doc = word.Documents.Open(r'path to your doc file')
  4. print doc.Words.Count
  5.  
You can however convert your Word document to unformatted text using the following code. And then you can use your word counter as for normal text files.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import *
  2. word = Dispatch("Word.Application")
  3. doc = word.Documents.Open(r'path to your doc file')
  4. print doc.Content.Text
  5.  
As pointed out in the previous message you need the win32com module. It is included in the PythonWin installation.

/Fredrik
Oct 26 '06 #3
bartonc
6,596 Expert 4TB
I thought that the following COM function calls would solve the problem, but it turns out that the Words.Count operation not just counts the number of words. It also counts the number of separators such as .,?

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import *
  2. word = Dispatch("Word.Application")
  3. doc = word.Documents.Open(r'path to your doc file')
  4. print doc.Words.Count
  5.  
You can however convert your Word document to unformatted text using the following code. And then you can use your word counter as for normal text files.

Expand|Select|Wrap|Line Numbers
  1. from win32com.client import *
  2. word = Dispatch("Word.Application")
  3. doc = word.Documents.Open(r'path to your doc file')
  4. print doc.Content.Text
  5.  
As pointed out in the previous message you need the win32com module. It is included in the PythonWin installation.

/Fredrik
Thanks again, Fredrik,
Barton
Oct 31 '06 #4
bartonc
6,596 Expert 4TB
hungryjackers has reported this post:

Opening a word file in Python [not quite solved]
http://www.thescripts.com/forum/showthread.php?postid=2159114#post2159114

This is part of this thread:
Opening a word file in Python [not quite solved]
http://www.thescripts.com/forum/showthread.php?threadid=553455

This is the reason that the user gave:
hi can u send me the code
Feb 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: o'seally | last post by:
solaris/linux admins/rookie_developers that battle with this error are probably all frustrated when it happens. i bet you're also somehow frustrated by this seemingly unsolvable error :-) ...take...
5
by: Fazer | last post by:
Here comes another small question from me :-) I am curious as to how I should approach this issue. I would just want to parse simple text and maybe perhaps tables in the future. Would I have to...
3
by: news.west.cox.net | last post by:
I want to check a directory, see if there are any files in it... then open each one and do something. I have this... files = os.listdir('/direcory') if len(files) > 0: for file in files: f1...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
5
by: Vinay | last post by:
Hi I have a corrupt word file. I am able to open it with the code given below tr Dim pInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo( pInfo.UseShellExecute...
4
by: Charlie Brookhart | last post by:
I am trying to write a code for a button click event. When the button is clicked, it is supposed to bring up an open file dialog box to allow the user to select the document they which to open....
6
by: Eric Layman | last post by:
Hi, I have fields from textareas. With a click of a button, php is able to grab these fields and by using header(), convert the output to Ms Word doc. But the outcome of the word doc...
7
by: giladp1 | last post by:
I found Albert Kallal's great "Super easy Word Merge" code in his site at: http://www.members.shaw.ca/AlbertKallal/msaccess/msaccess.html Thanks Albert so much for sharing this. I am looking...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.