[python-win32] Converting HTML formatting to Microsoft Word

sharpblade sharpblade1 at gmail.com
Tue Jun 14 10:51:05 CEST 2011

Ah yes, sorry I didn't specify that. It is part of an existing doc: I
have a Django web application with a Report model which contains
information I need to put into a word document. Most of this is simple
stuff requiring no formatting, and I achieve this by defining several
bookmarks within the master template and populate them like so:

doc.Bookmarks("prepared_for").Range.Text = job.customer.name
doc.Bookmarks("reference_UTN").Range.Text = job.UTN
doc.Bookmarks("test_type").Range.Text = job.get_type_display()

This works fine, but there are several fields (An appendix for
example) which may have text that needs to be formatted. The tl;dr of
it is that I need to parse the HTML and format the text using a Range
object accordingly.

I whipped up this function: http://pastebin.com/kaZz5dBd which should
work, but line 22 and 23 doesn't work for some reason - all text after
it is bold.

Anyone have any ideas?


On Tue, Jun 14, 2011 at 8:53 AM, Tim Golden <mail at timgolden.me.uk> wrote:
> On 14/06/2011 00:20, Tom wrote:
>> Hello,
>> I have a Javascript rich text editor (Specifically the Yahoo UI
>> libraries Simple Editor) which I have embedded within my web
>> application. This produces formatted HTML from the users input which
>> has to be placed within a Word document.
>> The number of formatting options is limited - Bold, italics,
>> underline, font size, font type, image insertion and lists. I thought
>> this would be rather simple to automate using words COM interface, but
>> I am having a lot of issues formatting the text using the COM
>> interface - my solution so far parses the HTML using BeautifulSoup and
>> rolls through each tag. If its a bold tag then it creates a range
>> object at the end of the current one, inserts the bold text and sets
>> the Bold attribute to True.
>> My issue is that this doesn't work and produces confusing results -
>> the text isn't formatted correctly and the Bold attribute is set to -1
>> (Which means a mix of both bold and non bold as far as I can tell).
>> Does anyone have any experience doing anything similar to this, or can
>> anyone give me a simpler way to do this?
> It's not 100% clear whether what I'm about to suggest will help but...
> Word will quite happily open (and edit) HTML directly. ie you can
>  File > Open > http://timgolden.me.uk/python/index.html
> and the result will be an editable version of that webpage.
> What's not clear is whether this would serve your purpose or
> how you're getting the "formatted HTML" from your YUI editor
> to the Word doc, and whether it forms part of an existing
> doc.
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32

More information about the python-win32 mailing list