Hi there,
I have a word document containing pictures and text. This documents
holds several 'ABCDEF' strings which serve as a placeholder for names.
Now I want to replace these occurences with names in a list (members). I
open both input and output file in binary mode and do the
transformation. However, I can't open the resulting file, Word just
telling that there was an error. Does anybody what I am doing wrong?
Oh, and is this approach pythonic anyway? (I have a strong Java background.)
Regards,
antoine
import os
members = somelist
os.chdir(somefolder)
doc = file('ttt.doc', 'rb')
docout = file('ttt1.doc', 'wb')
counter = 0
for line in doc:
while line.find('ABCDEF') -1:
try:
line = line.replace('ABCDEF', members[counter], 1)
docout.write(line)
counter += 1
except:
docout.write(line.replace('ABCDEF', '', 1))
else:
docout.write(line)
doc.close()
docout.close() 10 1948
Antoine De Groote wrote:
I have a word document containing pictures and text. This documents
holds several 'ABCDEF' strings which serve as a placeholder for names.
Now I want to replace these occurences with names in a list (members). I
open both input and output file in binary mode and do the
transformation. However, I can't open the resulting file, Word just
telling that there was an error. Does anybody what I am doing wrong?
The Word document format probably contains some length information about
paragraphs etc. If you change a string to another one of a different
length, this length information will no longer match the data and the
document structure will be hosed.
Possible solutions:
1. Use OLE automation (in the python win32 package) to open the file in
Word and use Word search and replace. Your script could then directly
print the document, which you probably have to do anyway.
2. Export the template document to RTF. This is a text format and can be
more easily manipulated with Python.
for line in doc:
I don't think that what you get here is actually a line of you document.
Due to the binary nature of the format, it is an arbitrary chunk.
Daniel
Antoine De Groote wrote:
Hi there,
I have a word document containing pictures and text. This documents
holds several 'ABCDEF' strings which serve as a placeholder for names.
Now I want to replace these occurences with names in a list (members).
Do you know that MS Word already provides this kind of features ?
I
open both input and output file in binary mode and do the
transformation. However, I can't open the resulting file, Word just
telling that there was an error. Does anybody what I am doing wrong?
Hand-editing a non-documented binary format may lead to undesirable
results...
Oh, and is this approach pythonic anyway?
The pythonic approach is usually to start looking for existing
solutions... In this case, using Word's builtin features and Python/COM
integration would be a better choice IMHO.
(I have a strong Java
background.)
Nobody's perfect !-)
Regards,
antoine
import os
members = somelist
os.chdir(somefolder)
doc = file('ttt.doc', 'rb')
docout = file('ttt1.doc', 'wb')
counter = 0
for line in doc:
Since you opened the file as binary, you should use file.read() instead.
Ever wondered what your 'lines' look like ?-)
while line.find('ABCDEF') -1:
..doc is a binary format. You may find such a byte sequence in it's
content in places that are *not* text content.
try:
line = line.replace('ABCDEF', members[counter], 1)
docout.write(line)
You're writing back the whole chunk on each iteration. No surprise the
resulting document is corrupted.
counter += 1
seq = list("abcd")
for indice, item in enumerate(seq):
print "%02d : %s" % (indice, item)
except:
docout.write(line.replace('ABCDEF', '', 1))
else:
docout.write(line)
doc.close()
docout.close()
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Antoine De Groote wrote:
Hi there,
I have a word document containing pictures and text. This documents
holds several 'ABCDEF' strings which serve as a placeholder for names.
Now I want to replace these occurences with names in a list (members). I
open both input and output file in binary mode and do the
transformation. However, I can't open the resulting file, Word just
telling that there was an error. Does anybody what I am doing wrong?
Oh, and is this approach pythonic anyway? (I have a strong Java background.)
Regards,
antoine
import os
members = somelist
os.chdir(somefolder)
doc = file('ttt.doc', 'rb')
docout = file('ttt1.doc', 'wb')
counter = 0
for line in doc:
while line.find('ABCDEF') -1:
try:
line = line.replace('ABCDEF', members[counter], 1)
docout.write(line)
counter += 1
except:
docout.write(line.replace('ABCDEF', '', 1))
else:
docout.write(line)
doc.close()
docout.close()
Errr.... I wouldn't even attempt to do this; how do you know each
'line' isn't going to be split arbitarily, and that 'ABCDEF' doesn't
happen to be part of an image. As you've noted, this is binary data so
you can't assume anything about it. Doing it this way is a Bad Idea
(tm).
If you want to do something like this, why not use templated HTML, or
possibly templated PDFs? Or heaven forbid, Word's mail-merge facility?
(I think MS Office documents are effectively self-contained file
systems, so there is probably some module out there which can
read/write them).
Jon.
Thank you all for your comments.
I ended up saving the word document in XML and then using (a slightly
modified version of) my script of the OP. For those interested, there
was also a problem with encodings.
Regards,
antoine
Bruno Desthuilliers wrote:
Antoine De Groote wrote:
>Hi there,
I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with names in a list (members).
Do you know that MS Word already provides this kind of features ?
No, I don't. Sounds interesting... What is this feature called?
>
>I open both input and output file in binary mode and do the transformation. However, I can't open the resulting file, Word just telling that there was an error. Does anybody what I am doing wrong?
Hand-editing a non-documented binary format may lead to undesirable
results...
>Oh, and is this approach pythonic anyway?
The pythonic approach is usually to start looking for existing
solutions... In this case, using Word's builtin features and Python/COM
integration would be a better choice IMHO.
>(I have a strong Java background.)
Nobody's perfect !-)
>Regards, antoine
import os
members = somelist
os.chdir(somefolder)
doc = file('ttt.doc', 'rb') docout = file('ttt1.doc', 'wb')
counter = 0
for line in doc:
Since you opened the file as binary, you should use file.read() instead.
Ever wondered what your 'lines' look like ?-)
> while line.find('ABCDEF') -1:
.doc is a binary format. You may find such a byte sequence in it's
content in places that are *not* text content.
> try: line = line.replace('ABCDEF', members[counter], 1) docout.write(line)
You're writing back the whole chunk on each iteration. No surprise the
resulting document is corrupted.
> counter += 1
seq = list("abcd")
for indice, item in enumerate(seq):
print "%02d : %s" % (indice, item)
> except: docout.write(line.replace('ABCDEF', '', 1)) else: docout.write(line)
doc.close() docout.close()
Antoine De Groote wrote:
Bruno Desthuilliers wrote:
>Antoine De Groote wrote:
>>Hi there,
I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with names in a list (members).
Do you know that MS Word already provides this kind of features ?
No, I don't. Sounds interesting... What is this feature called?
I don't know how it's named in english, but in french it's (well - it
was last time I used MS Word, which is quite some times ago???) "fusion
de documents".
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Antoine De Groote wrote:
Bruno Desthuilliers wrote:
>>Antoine De Groote wrote:
>>>Hi there,
I have a word document containing pictures and text. This documents holds several 'ABCDEF' strings which serve as a placeholder for names. Now I want to replace these occurences with names in a list (members).
Do you know that MS Word already provides this kind of features ?
No, I don't. Sounds interesting... What is this feature called?
Mail-merge, I believe.
However, if your document can be adequately represented in RTF
(rich-text format) then you could consider doing string replacements on
that. I invoice the PyCon sponsors using this rather inelegant technique.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
[Antoine]
I have a word document containing pictures and text. This documents
holds several 'ABCDEF' strings which serve as a placeholder for names.
Now I want to replace these occurences with names in a list (members).
[Bruno]
I don't know how it's named in english, but in french it's (well - it
was last time I used MS Word, which is quite some times ago???) "fusion
de documents".
"Mail Merge"?
--
Richie Hindle ri****@entrian.com
Antoine De Groote wrote:
Hi there,
I have a word document containing pictures and text. This documents
holds several 'ABCDEF' strings which serve as a placeholder for names.
Now I want to replace these occurences with names in a list (members). I
open both input and output file in binary mode and do the
transformation. However, I can't open the resulting file, Word just
telling that there was an error. Does anybody what I am doing wrong?
Oh, and is this approach pythonic anyway? (I have a strong Java background.)
Regards,
antoine
import os
members = somelist
os.chdir(somefolder)
doc = file('ttt.doc', 'rb')
docout = file('ttt1.doc', 'wb')
counter = 0
for line in doc:
while line.find('ABCDEF') -1:
try:
line = line.replace('ABCDEF', members[counter], 1)
docout.write(line)
counter += 1
except:
docout.write(line.replace('ABCDEF', '', 1))
else:
docout.write(line)
doc.close()
docout.close()
DOC files contain housekeeping info which becomes inconsistent if you
change text. Possibly you can exchange stuff of equal length but that
wouldn't serve your purpose. RTF files let you do substitutions and they
save a lot of space too. But I kind of doubt whether RTF files can
contain pictures.
Frederic
Frederic Rentsch <an***********@vtxmail.chwrote:
DOC files contain housekeeping info which becomes inconsistent if you
change text. Possibly you can exchange stuff of equal length but that
wouldn't serve your purpose. RTF files let you do substitutions and they
save a lot of space too. But I kind of doubt whether RTF files can
contain pictures.
They wouldn't be a lot of use as a document file format if they couldn't
contain pictures. RTF files can contain just about anything, they can even
embed other non-rtf objects. Whether rtf applications apart from Word can
actually handle all of the tags is, of course, another question. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: S Taylor |
last post by:
I am running MSWord VBA code from within Access VBA that merges a Word mail
merge document to the printer, using data in Access.
In Office 97 it worked fine, but in Word 2003 a new message comes up...
|
by: Andrew |
last post by:
I'm adding this as it to me a while to figure out all the pieces to be
able to do this without using Microsoft.Office.Interop which caused me
problems on the web-server.
Streaming is the easy...
|
by: Crirus |
last post by:
dim pp as string
pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256,
Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156,
Y=200.7715}{X=200.7715, Y=156}{X=256,...
|
by: jacob navia |
last post by:
Hi guys!
I like C because is fun. So, I wrote this function for the lcc-win32
standard library: strrepl.
I thought that with so many "C heads" around, maybe we could improve it
in a...
|
by: Fabian |
last post by:
Hello,
I want to open a Word Document in my C# Programm. I tried this :
Word.Application wordapp = new Word.Application();
object path = "TEST.DOC";
object vk_read_only = false;
object...
|
by: gregpinero |
last post by:
Hi guys,
What I'm trying to do is find all instances of an acronymn such as IBM
on a webpage and replace it with <acronym title="International Business
Machines">IBM</acronym>. However in my...
|
by: scottyman |
last post by:
I can't make this script work properly. I've gone as far as I can with
it and the rest is out of my ability. I can do some html editing but
I'm lost in the Java world. The script at the bottom of...
|
by: senthilavs |
last post by:
Hi,
Im having word document in the Server Machine. While im trying to open the file in Client Machine file is opening in Server only. I need to open in the client only. This is an ASP.NET project...
|
by: etuncer |
last post by:
Hello All,
I have Access 2003, and am trying to build a database for my small
company. I want to be able to create a word document based on the data
entered through a form. the real question is...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: WisdomUfot |
last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |