http://www.perlmonks.org?node_id=457283


in reply to Word HTML issues

Yuck. Below is a script that i used recently to script out a lot of word-generated junk .. caveat emptor--it was a quick & dirty solution for my specific files. But some of the regex's maybe of some use. Note that it gets rid of everything inbetween <!...> tags, and also pretty much strips also style junk with mso in it.
Usage: cleanWord.pl infile > outfile
#!/usr/bin/perl -p0777 s#<!--.*?-->##sg; s#<!\[if .*?\]>(.*?(?!<!).*?)<!\[endif\](--)?>##sg; s#<o:p></o:p>##sg; s#<span\s+style='(?:(?:mso-bidi-)?font-size:\d+\.\d+pt;\s*)?font-famil +y:Arial'>([^<]*)</span>#$1#sg; s#<ol start=1 type=1>#<ol>#sg; s#<li[^>]+>#<li>#sg; s#<span\s+style='(?:(?:mso-bidi-)?font-size:\d+\.\d+pt;\s*)?font-famil +y:Arial'>#<span>#sg; s#<span\s+style="mso-spacerun:\s*yes">#<span>#sg; s#<b style='mso-bidi-font-weight:\s*normal'>#<b>#sg; s#<p[^>]*?>#<p>#sg; s#<span[^>]*?>(.*?(?!span).*?)</span>#$1#sg; s#<span>(.*?(?!span).*?)</span>#$1#sg; # s#(<img.*?src=")[^>]+/[^>]+\.([^\.>]+?)(">)#sprintf("%simage%04d.%s% +s",$1,++$N,$2,$3)#sge; s#(<img.*?\s+)id="\S+"([^>]*?>)#$1$2#sg; s#class=section#class="Section"#sg;

As for a more generic approach, I haven't used one, but a quick cpan search or HTML yields HTML::Scrubber and HTML::Sanitizer which (at a 2-s glance) look promising.

Replies are listed 'Best First'.
Re^2: Word HTML issues
by Animator (Hermit) on May 15, 2005 at 20:57 UTC

    The OP changed his question into:

    How does that script work with all the # in it?

    I assume this was ment to be a reply to this post, so I'll post my answer here.

    The # in s#class=section#class="Section"#sg; for example is used as a regex-delimiter, not as a comment. It is the same as s/class=section/class="Section"/sg; except that with s/// you would need to escape the / (which could make it less readable, but that does not apply in this case since it has no / in the regex)