Rename Built-in Styles





Rename Built-in Styles

figs/moderate.gif figs/hack54.gif

Word is often merely the input phase in a production line that ends with a more formidable layout package, such as Adobe InDesign or QuarkXPress. This hack shows you how to make Word documents more palatable to other programs.

When Word is part of a production workflow, to work with it optimally would impose some restrictions on other parts of the workflow. For example, your company's designers probably prefer their own styles to Word's. Those styles may have identical names to Word's built-in styles, but chances are they don't.

Many choose (or are forced) to create custom styles in Word that correspond to those used by the layout software. But that means missing out on some of the advantages of using Word's built-in styles, such as being able to use Outline view. The other (equally inconvenient) choice is to stick with Word's styles and then perform a bunch of Find and Replace operations on the files after you import them into the layout software. Either option is a drag on your workflow.

Let's look at an example. Say you turn over some Word files for layout to a design staff that uses InDesign. They've got an extensive set of templates, and in those templates the styles for the first four heading levels are named HeadA, HeadB, HeadC, and HeadD. Typically, they import Word files directly into InDesign, then take the time to replace Heading 1 with HeadA, Heading 2 with HeadB, and so forth. Then they delete the imported Word heading styles from the InDesign document. It would certainly be easier if you named your heading styles the same as theirs, but again, that means losing out on the benefits of using Word's built-in heading styles.

While you can't change Word's built-in style names, you can change anything you want about an RTF file, which, after all, is just plain text. And whatever layout program you use (Quark, InDesign, or FrameMaker) can definitely import an RTF file.

The trick is to save the files as RTF, then modify Word's style names to match those of the layout software. Then, when you open the documents in the other program, the text will assume the formatting defined by the styles with those names in that program.

After you modify the RTF files, import them into the layout software—don't reopen them in Word. If you reopen them in Word, any built-in styles will be recreated, which may cause undesired results (at the very least, Word will recreate all the built-in styles you just renamed).


Continuing with the example above, to change the Heading 1 style to HeadA, the Heading 2 style to HeadB, and so on, you'll first need to get your file into RTF format by selecting FileSave As and choosing "Rich Text Format" from the "Save as type" field. Next, open the file in a standard text editor such as Notepad.

RTF stores information about each of the styles in a document in a place called the style table. Everything about a style is listed there, from its name to its indent amount. Each style is also assigned a number, such as \s2. Whenever text in the document uses a style, the number notes which style should be applied. That means you only need to change the style's name in the style table; everywhere else in the document, the style is referenced by number, not by name. A sample style table entry for the Heading 1 style in a Word document is shown in Figure.

The Heading 1 style defined in an RTF file
figs/wrdh_0607.gif


If you change "Heading 1" in the RTF file to "HeadA," when you open the file in InDesign, any text that used the Heading 1 style in Word will be formatted with the HeadA style defined in InDesign.

1 Hacking the Hack

While editing RTF files by hand can be quite educational, it's also a bit tedious. It's better to relegate the dirty work to a script, such as the following Perl script, which changes Word's built-in Heading 1, Heading 2, Heading 3, and Heading 4 styles to HeadA, HeadB, HeadC, and HeadD in an RTF file.

This section assumes that your have Perl installed on your system and that you can run Perl scripts from the DOS command line. To download a free version of Perl for Windows, go to the ActiveState web site at ttp://www.activestate.com.


This script uses the RTF::Parser module. If you're running the ActivePerl distribution for Windows, you can install RTF::Parser from the Perl Package Manager. You can also download RTF::Parser from http://www.cpan.org. Save this script in the same folder as the file you're modifying and name it changestyles.pl.

#!/usr/bin/perl



use RTF::Tokenizer;



my $filename = shift;



my $tokenizer = RTF::Tokenizer->new( file => $filename);



while( my ( $type, $arg, $param ) = $tokenizer->get_token( ) ){

    last if $type eq 'eof';



    if($type eq 'control' and $arg eq 'stylesheet') {

        put( $type, $arg, $param );



        while( my @args = $tokenizer->get_token( ) ) {

            for (@args) {

                $_ =~ s/Heading 1/HeadA/i;

                $_ =~ s/Heading 2/HeadB/i;

                $_ =~ s/Heading 3/HeadC/i;

                $_ =~ s/Heading 4/HeadD/i;

            }



            put( @args );



            last if $args[0] eq 'control' and $args[1] eq 'generator';

        }

    } else {

        put( $type, $arg, $param );

    }

}



sub put {

    my ($type, $arg, $param) = @_;



    if( $type eq 'group' ) {

        print $arg == 1 ? '{' : '}';

    } elsif( $type eq 'control' ) {

        print "\\$arg$param";

    } elsif( $type eq 'text' ) {

        print "\n$arg";

    }

}

On Windows, you'd run the script from a DOS prompt:

> perl changestyles.pl MyFile.rtf

For more on hacking Word from Perl, check out [Hack #86] .



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows