473,320 Members | 2,006 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,320 software developers and data experts.

using python to edit a word file?

I figured my first step is to install the win32 extension, which I did,
but I can't seem to find any documentation for it. A couple of the links
on Mark Hammond's site don't seem to work.

Anyway, all I need to do is search in the Word document for certain
strings and either delete them or replace them. Easy enough, if only I
knew which function, etc. to use.

Hope someone can push me in the right direction.

Thanks.
Aug 10 '06 #1
8 4182
John Salerno <jo******@NOSPAMgmail.comwrites:
I figured my first step is to install the win32 extension, which I
did, but I can't seem to find any documentation for it. A couple of
the links on Mark Hammond's site don't seem to work.

Anyway, all I need to do is search in the Word document for certain
strings and either delete them or replace them. Easy enough, if only I
knew which function, etc. to use.

Hope someone can push me in the right direction.
Maybe this will be helpful:

http://aspn.activestate.com/ASPN/Coo.../Recipe/279003

--
Regards,
Rob
Aug 10 '06 #2
Rob Wolfe wrote:
John Salerno <jo******@NOSPAMgmail.comwrites:
>I figured my first step is to install the win32 extension, which I
did, but I can't seem to find any documentation for it. A couple of
the links on Mark Hammond's site don't seem to work.

Anyway, all I need to do is search in the Word document for certain
strings and either delete them or replace them. Easy enough, if only I
knew which function, etc. to use.

Hope someone can push me in the right direction.

Maybe this will be helpful:

http://aspn.activestate.com/ASPN/Coo.../Recipe/279003
But if I save the file to text, won't it lose its formatting?

More specifically, here's what I have: a four-page calendar, each page
with three months on it. The months are in tables, which is why I don't
think making a text file will help me here, because I'll lose all that.
What I need to do is renumber all the dates, basically replacing a
number with itself minus 1. So it's not a simple find/replace task, and
there doesn't seem to be a way to do this in Word's find/replace feature
(but if there is, please let me know!)
Aug 10 '06 #3
On 2006-08-10 15:15:34, John Salerno wrote:
I figured my first step is to install the win32 extension, which I did,
but I can't seem to find any documentation for it. A couple of the links
on Mark Hammond's site don't seem to work.

Anyway, all I need to do is search in the Word document for certain
strings and either delete them or replace them. Easy enough, if only I
knew which function, etc. to use.

Hope someone can push me in the right direction.
When Word is installed, you have a few COM interfaces to Word. I'm not sure
how to access these with Python (but documentation about using COM with
Python should help you here), and I'm not sure whether what you want is
available (but the Word COM documentation should help you with that).

Gerhard

Aug 10 '06 #4
John Salerno wrote:
But if I save the file to text, won't it lose its formatting?
It looks like I can save it as an XML file and it will retain all the
formatting. Now I just need to decipher where the dates are in all that
mess and replace them, just using a normal text file! :)
Aug 10 '06 #5
John Salerno wrote:
I figured my first step is to install the win32 extension, which I did,
but I can't seem to find any documentation for it. A couple of the links
on Mark Hammond's site don't seem to work.

Anyway, all I need to do is search in the Word document for certain
strings and either delete them or replace them. Easy enough, if only I
knew which function, etc. to use.

Hope someone can push me in the right direction.

Thanks.
The easiest way for me to do things like this is to do it in Word and
record a VB Macro. For instance you will see something like this:

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "save it"
.Replacement.Text = "dont save it"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

and then hand translate it to Win32 Python, like:

wordApp = Dispatch("Word.Application")
wordDoc=wordApp.Documents.Add(...some word file name...)
wordRange=wordDoc.Range(0,0).Select()
sel=wordApp.Selection
sel.Find.ClearFormatting()
sel.Find.Replacement.ClearFormatting()
sel.Find.Text = "save it"
sel.Find.Replacement.Text = "dont save it"
sel.Find.Forward = True
sel.Find.Wrap = constants.wdFindContinue
sel.Find.Format = False
sel.Find.MatchCase = False
sel.Find.MatchWholeWord = False
sel.Find.MatchByte = False
sel.Find.CorrectHangulEndings = False
sel.Find.MatchAllWordForms = False
sel.Find.MatchSoundsLike = False
sel.Find.MatchWildcards = False
sel.Find.MatchFuzzy = False
sel.Find.Find.Execute(Replace=constants.wdReplaceA ll)
wordDoc.SaveAs(...some word file name...)

Can't say that this works as I typed because I haven't try it myself
but should give you a good start.

Make sure you run the makepy.py program in the
\python23\lib\site-packages\win32com\client directory and install the
"MS Word 11.0 Object Library (8.3)" (or something equivalent). On my
computers, this is not installed automatically and I have to remember
to do it myself or else things won't work.

Good Luck.

Aug 10 '06 #6
John,

I have a notion about translating stuff in a mess and could help you with the translation. But it may be that the conversion
from DOC to formatted test is a bigger problem. Loading the files into Word and saving them in a different format may not be a
practical option if you have many file to do. Googling for batch converters DOC to RTF I couldn't find anything.
If you can solve the conversion problem, pass me a sample file. I'll solve the translation problem for you.

Frederic
----- Original Message -----
From: "John Salerno" <jo******@NOSPAMgmail.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Thursday, August 10, 2006 9:08 PM
Subject: Re: using python to edit a word file?

John Salerno wrote:
But if I save the file to text, won't it lose its formatting?

It looks like I can save it as an XML file and it will retain all the
formatting. Now I just need to decipher where the dates are in all that
mess and replace them, just using a normal text file! :)
--
http://mail.python.org/mailman/listinfo/python-list
Aug 11 '06 #7
Anthra Norell wrote:
John,

I have a notion about translating stuff in a mess and could help you with the translation. But it may be that the conversion
from DOC to formatted test is a bigger problem. Loading the files into Word and saving them in a different format may not be a
practical option if you have many file to do. Googling for batch converters DOC to RTF I couldn't find anything.
If you can solve the conversion problem, pass me a sample file. I'll solve the translation problem for you.

Frederic
What I ended up doing was just saving the Word file as an XML file, and
then writing a little script to process the text file. Then when it
opens back in Word, all the formatting remains. The script isn't ideal,
but it did the bulk of changing the numbers, and then I did a few things
by hand. I love having Python for these chores! :)

import re

xml_file = open('calendar.xml')
xml_data = xml_file.read()
xml_file.close()

pattern = re.compile(r'<w:t>(\d+)</w:t>')

def subtract(match_obj):
date = int(match_obj.group(1)) - 1
return '<w:t>%s</w:t>' % date

new_data = re.sub(pattern, subtract, xml_data)

new_file = open('calendar2007.xml', 'w')
new_file.write(new_data)
new_file.close()
Aug 11 '06 #8
No one could do it any better. Good for you! - Frederic

----- Original Message -----
From: "John Salerno" <jo******@NOSPAMgmail.com>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Friday, August 11, 2006 4:08 PM
Subject: Re: using python to edit a word file?

Anthra Norell wrote:
John,

I have a notion about translating stuff in a mess and could help you with the translation. But it may be that the
conversion
from DOC to formatted test is a bigger problem. Loading the files into Word and saving them in a different format may not be a
practical option if you have many file to do. Googling for batch converters DOC to RTF I couldn't find anything.
If you can solve the conversion problem, pass me a sample file. I'll solve the translation problem for you.

Frederic

What I ended up doing was just saving the Word file as an XML file, and
then writing a little script to process the text file. Then when it
opens back in Word, all the formatting remains. The script isn't ideal,
but it did the bulk of changing the numbers, and then I did a few things
by hand. I love having Python for these chores! :)

import re

xml_file = open('calendar.xml')
xml_data = xml_file.read()
xml_file.close()

pattern = re.compile(r'<w:t>(\d+)</w:t>')

def subtract(match_obj):
date = int(match_obj.group(1)) - 1
return '<w:t>%s</w:t>' % date

new_data = re.sub(pattern, subtract, xml_data)

new_file = open('calendar2007.xml', 'w')
new_file.write(new_data)
new_file.close()
--
http://mail.python.org/mailman/listinfo/python-list
Aug 11 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Daniel Cloutier | last post by:
Hi, is it possible to edit or write Word-files out of a Python-Program? thx in advance daniel
2
by: Russell E. Owen | last post by:
I'm trying to build Python 2.3.4 from source on a RedHat Enterprise machine for installation in a net-wide accessible directory /net/python. I tried all of the following variants of ./configure...
10
by: Paul Kooistra | last post by:
I need a tool to browse text files with a size of 10-20 Mb. These files have a fixed record length of 800 bytes (CR/LF), and containt records used to create printed pages by an external company. ...
17
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows...
1
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working...
0
by: napolpie | last post by:
DISCUSSION IN USER nappie writes: Hello, I'm Peter and I'm new in python codying and I'm using parsying to extract data from one meteo Arpege file. This file is long file and it's composed by...
1
by: Chris Carlen | last post by:
Hi: I'm writing a Python program, a hex line editor, which takes in a line of input from the user such as: -e 01 02 "abc def" 03 04 Trouble is, I don't want to split the quoted part where...
3
by: Eric_Dexter | last post by:
I am trying to take some data in file that looks like this command colnum_1 columnum_2 and look for the command and then cange the value in the collum(word) number indicated. I am under...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.