Categories > OpenTBS with DOCX >

Setting docx styles

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Chris
Date: 2013-07-10
Time: 18:15

Setting docx styles

To give the background, I'm attempting to convert an exisiting docx template to be updated through a web system.

The current template has several header sections - which use a 'style', where font color/size and bg color are all defined. Is there anyway to get at these 'styles' to modify them through OpenTBS, or is it, as I think, not possible?

If it isn't possible through OpenTBS, I presume I'll have to XML-it in. (I'm already loading my template with OPENTBS_ALREADY_XML, to deal with linebreaks.) Is anybody able to give me any pointers to how I could handle this with XML tags - would it be possible simply override the predefined style (I suspect not), or would I need to look at wrapping my header section texts in xml style tags. (Is that how it works, in a HTML-esque way?)

Much Thanks,
By: Sarah Kemp
Date: 2013-07-10
Time: 18:52

Re: Setting docx styles

The style information is stored in the docx under word/styles.xml so you would want to load this file in addition to the main template:

$TBS->LoadTemplate('path/to/your/template.docx');
$TBS->LoadTemplate('#word/styles.xml');

You will have to get into the XML that file to add your TBS tags, AFAIK. (This is all assuming you have some need to edit the styles each time you use the template, if you are just looking to change them once you can do that in Word)

If you aren't looking to change the list of styles available to the document (like, the Quick Styles), but just change the font or color of a given area of the document, you won't need to get into that file. Just take a look at the XML of your main template with the different styles you are looking for and target the necessary attributes on a per-case basis. (Create two copies of your template, one as default and one looking how you want, change them to zip files and look at word/document.xml to see what is different - a compare script is useful for this... as well as an XML formatter).

I'm not really clear on your goal here, but hopefully this will get you started?
By: Chris
Date: 2013-07-12
Time: 12:59

Re: Setting docx styles

Hi Sarah,

Thanks for the above, it's given me a basis. I guess I don't really need to change the Quick Styles, I can just style the individual paragraphs, as it doesn't need to be available to the user afterwards. I've done a couple of my templates, and through manually extracting the XML files in Windows explorer, seen how I'd need to edit the document.xml file.

What I'm now stuck with, how the heck do I get at these XML files through TBS? Is there a way to return something like an XML Object or String from a loaded template? All I seem to be able to get hold of is a list of the field names I've put in myself. (e.g. with GetBlockSource() or $tbs->Source)

Thanks
By: Sarah Kemp
Date: 2013-07-12
Time: 16:55

Re: Setting docx styles

Personally, I've given up template editability in Word and just place my TBS tags directly in the XML.

I think you can accessed blocks outside of where your tag is currently located with extended blocks (http://www.tinybutstrong.com/manual.php#html_block_prm_extblk). You can also try making the w:r your block and filling in all the style parameters you want with xml and the parameter strconv=none (might be 'no' or 'false... can't remember). If you are changing colors you may need the 'att' field parameter.

Hope I don't lead you astray with this, as I said I work in the XML so I'm no authority on how well these suggestions might work...
By: Chris
Date: 2013-07-15
Time: 12:52

Re: Setting docx styles

"Personally, I've given up template editability in Word and just place my TBS tags directly in the XML. "

*facepalm* That alone answered my question here, of course I can just put the tags into the XML file, and rezip it. Thick moment...sorry!


So, Work is carrying on. After several hours, I've worked out that I have an issue in that I'm attempting to format a text which is being input through a TBS tag, which I think is overwriting the whole paragraph tag, and as such, any changes I was trying to make to that area were being lost. Instead, it makes more sense for me to edit the 'Heading 1' style, as that is still applied to the paragraph. My issue now is, I don't seem to be able to load multiple templates at once. (In doing so, I'm getting no error messages, however none of my fields are being merged.)

This is how I'm trying to do it, am I doing something stupid?

    //Load the template
    $template = 'WorkingXML.docx';
    $TBS->LoadTemplate($template,OPENTBS_ALREADY_XML); // Also merge some [onload] automatic fields (depends of the type of document).
    $TBS->LoadTemplate('#word/styles.xml');
    return $TBS;

And all I'm doing to the $TBS after that point is...

$TBS->MergeBlock('section', $sections);
$TBS->Show(OPENTBS_DOWNLOAD, 'sample.docx');

Any ideas? I'm at a loss...again! Sorry...I feel like I may be around a bit on these forums over the next couple of weeks.

(And for full info, this is what I'm trying to change in the styles.xml page, does this look syntactically correct?)

<w:color w:val="FFFFFF" />
            [onload.fontHex;att=w:color#w:val]
Thanks.
By: Sarah Kemp
Date: 2013-07-15
Time: 17:45

Re: Setting docx styles

Nothing sticks out as stupid to me. Your syntax looks valid. I do my merge block after my main template load and before my second template load, though I don't know if that would make a difference. My second template doesn't have any blocks, only onshows. Have you tried $TBS->Show(OPENTBS_DEBUG_XML) to see if your tags are merging empty or just not merging at all? Do you mean that none of your tags in the main template AND in the styles template are merging or just the styles?
By: Chris
Date: 2013-07-15
Time: 18:15

Re: Setting docx styles

(Apologies...I've been typing this post over the last half an hour, with every new thing I try, so it reads a bit like a diary! But I'm there!)

No, nothing was merging in the main template either. Just tried moving the order about with the merge blocks, and that does nothing. :(

I have just noticed one thing, when I was looking at that however - it seems to be that when I resave my doc as a docx, from editing it as a zip, it resets the fields in the styles file. Is there any way around this? Have I been doing it the wrong way all along?

My process:
- Unzip the docx with 7-zip
- Edit the styles.xml file
- Rezip the folders with 7-zip
- Open the .zip in Word, and save as a docx from there.

Re the last bullet point, I tried just renaming the file extension, however OpenTBS wasn't able to find it. Looking deeper into the properties...the beautiful windows operating system had been hiding the ugly file extensions, so the file was actually names template.docx.zip. (This is all really obvious when I think about it. :( ) By renaming the file through the command line, I was able to get OpenTBS to locate it, AND not have to open it with word which resets the tags. Now I'll just never open it with word again I guess!

Thanks for all your help!
By: Sarah Kemp
Date: 2013-07-15
Time: 18:58

Re: Setting docx styles

I can't tell you how many posts I have written to this forum that are far longer and less legible than yours.

I am not surprised that Word overwrites your changes when you save, as I said, I gave up editablility in Word when I put my tags in the XML. That said, there maybe hope for preserving your style changes if you basically ignore whatever Word wants to put in your styles.xml file and just use an XML copy (of styles.xml) with your docx template. I don't know if you've read about "manually" replacing files in the archive with OpenTBS, but I think you could replace the styles.xml file in the template with a clean copy you keep separate via OpenTBS: http://www.tinybutstrong.com/plugins/opentbs/tbs_plugin_opentbs.html#modify_files

I haven't tried this, and I don't know how well it would work with merging tags - you may need to load and merge the clean styles.xml first, get it as an XML string and then use the OPENTBS_REPLACEFILE - unfortunately I have no experience with this to offer. If you do try it and get it working I hope you will share your secrets.
By: Chris
Date: 2013-07-16
Time: 10:33

Re: Setting docx styles

As I can't edit my previous post, an update: the order of loading templates DOES make a difference. In my project, it appears that I have to load my styles template AFTER I've done the block merges for my main template.

And thanks for that link, I need to catch up on some other parts first, but I could use something like that in the final implementation, so will update when I get round to it. Cheers!
By: Chris
Date: 2013-07-16
Time: 16:23

Re: Setting docx styles

Ok, so the need came up earlier then I though. Straight away I'm stuck with the syntax of OPENTBS_REPLACEFILE - I'm unable to 'find my file'. Can anyone explain the syntax I should be using for the file I want to target? (2nd Param) I'm not entirely sure what the API means by 'inner path'.

(To clarify, I'm trying to replace the styles.xml file in WorkingXML.docx, with styles.xml in my current dir.)

$TBS->Plugin(OPENTBS_REPLACEFILE, 'WorkingXML.docx#word/styles.xml', 'styles.xml', $DataType=OPENTBS_FILE, $Compress=true);

I've also tried '#word/styles.xml', the format I'd use to load it as a template. Besides that, I'm not really sure what else to try. =/

Thanks in advance.
By: Sarah Kemp
Date: 2013-07-16
Time: 16:57

Re: Setting docx styles

Again, I'm no authority here - hopefully if someone knows better they will correct me.

I would try the $TBS->Plugin(OPENTBS_FILEEXISTS, $Name) to verify you have the correct path to the file you are replacing:
$TBS->Plugin(OPENTBS_FILEEXISTS, word/styles.xml); //(I've not included a # this time because the example in the manual doesn't use one)

If you can confirm the existence of the file at the expected path, then you only have to worry about the path to your replacement file (which looks right to me but only time will tell).
By: Chris
Date: 2013-07-17
Time: 11:26

Re: Setting docx styles

That was a simple solution! After this, after messing around a bit, I ended up doing just about exactly what you suggested, merging styles.xml in a fresh TBS class separately, and replacing the words/styles.xml file with an XML string.

The code:
$TBS2 = new clsTinyButStrong;
$TBS2->LoadTemplate('styles.xml');
$TBS2->Show(TBS_NOTHING);
$xmlStr = $TBS2->Source;

$TBS->Plugin(OPENTBS_REPLACEFILE, 'word/styles.xml', $xmlStr, $DataType=OPENTBS_STRING, $Compress=true);

$TBS->Show(OPENTBS_DOWNLOAD, 'sample.docx');

Thanks for all your help!