Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Will you be adding support for styles? #10

Open
razorgoto opened this issue Aug 1, 2018 · 11 comments
Open

Will you be adding support for styles? #10

razorgoto opened this issue Aug 1, 2018 · 11 comments
Labels
enhancement New feature or request

Comments

@razorgoto
Copy link

Is support for styles on the roadmap?

@PrzemyslawKlys
Copy link
Member

What you mean by Styles?

@razorgoto
Copy link
Author

https://msdn.microsoft.com/en-us/vba/word-vba/articles/style-object-word?f=255&MSPPError=-2147217396

Paragraph and character styles. Either have access to apply a existing style to a paragraph or create a new style.

@PrzemyslawKlys
Copy link
Member

Those are already there as far as I know. Unless you mean something completely different you can already set color, font size, heading1-9, and so on. Tables can have design. Not sure if you want something else?

@razorgoto
Copy link
Author

Yes, something different.

I am a technical writer. The standard practice for Microsoft Word is to define a set of style first, then use those styles for every paragraph. It's like the same idea as separation of concerns in web design. Instead of formatting the text with inline formatting, you would have a define CSS sheet for the fonts and colors of the items.

H1-H9 are builtin styles. It would be good to be able to define new styles and also be able to change the appearance of H1-H9.

So I think I am looking for something like:```

Add-WordParagraphStyle -WordDocument $WordDocument -Name 'LabelText' -FontSize 24 -Color Red -Bold $true
Add-WordParagraph -WordDocument $WordDocument -Text 'This is Label' -Style 'LabelText'
Set-WordStyleParagraph -WordDocument $WordDocument -Name 'Heading1' -FontSize 20 -Color Black
Add-WordParagraph -WordDocument $WordDocument -Style 'Heading1' -Text 'This is black and size 20'

@PrzemyslawKlys
Copy link
Member

PrzemyslawKlys commented Aug 2, 2018

Well there are 2 ways to do it.

  • The long way - I need to find out how to it :-) and whether it's already supported by C# version. I have seen some formatting option but not sure if that's it.

I can see this...

    ///<summary>
    /// The style name of the paragraph.
    ///</summary>
    public string StyleName
    {
      get
      {
        var element = this.GetOrCreate_pPr();
        var styleElement = element.Element( XName.Get( "pStyle", DocX.w.NamespaceName ) );
        var attr = styleElement?.Attribute( XName.Get( "val", DocX.w.NamespaceName ) );
        if ( !string.IsNullOrEmpty( attr?.Value ) )
        {
          return attr.Value;
        }
        return "Normal";
      }
      set
      {
        if( string.IsNullOrEmpty( value ) )
        {
          value = "Normal";
        }
        var element = this.GetOrCreate_pPr();
        var styleElement = element.Element( XName.Get( "pStyle", DocX.w.NamespaceName ) );
        if( styleElement == null )
        {
          element.Add( new XElement( XName.Get( "pStyle", DocX.w.NamespaceName ) ) );
          styleElement = element.Element( XName.Get( "pStyle", DocX.w.NamespaceName ) );
        }
        styleElement.SetAttributeValue( XName.Get( "val", DocX.w.NamespaceName ), value );
      }
    }

Which is being utilized by Heading1-9. I believe if you would create "styles" in template with this property we could utilize it. However I am not sure how to set it to proper style if you would like to build it from scratch.

  • The short way - I can write the code as below which will be a wrapper of sort.
$YourNewStyle = New-WordStyleParagraph -FontSize 20 -Color Black -HeadingType Heading1 

Add-WordText -Text 'This is it' -Style $YourNewStyle

$YourNewStyle would be a PSCustomObject which would have all the styles defined.

The problem with that it won't be available outside of PSWriteWord. So if you want to continue using same style in Word after generation ... it won't be there. It will require some work to implement but this could be done for "custom table styles" and any other things. But the ultimate problem with it .. behind the scenes I will be building this "manually". I won't be saving this as a style in Word.

@razorgoto
Copy link
Author

The Short Way is not really that great. After all, most of the time, you generate a DOCX file because you intend for it to be edited by other people. If I don't want others to edit or change it, I would send a PDF. But, this short way is still very useful in a pinch.

The Long Way is what I wanted.
The code snippet is mostly it. Being able to add styles to paragraphs and text is exactly what I want to do. So currently, I can just specify my own styles in PSWriteWord? I had thought that I can only use H1-H9.
It would be nice if PSWriteWord can also create a Style Object, but it isn't critical. User can always just make them in Word or hack the OOXML.

@PrzemyslawKlys
Copy link
Member

To be honest I've not tested any of it. Heading1-9 works as it's defined that way. I would need to do some testing with it. Maybe I will have some time during weekend. Currently I'm rewritting Add-WordTable to be consistent with what $Variable | Format-Table outputs. Right now Add-WordTable does conversions but this will change. It will not do conversion. It will deliver the same output that Format-Table does (well similar at least. But to still provide functionality of conversion (kind of pivot rows names with column names) I will deliver switch do to that.

@razorgoto
Copy link
Author

Doing table are just horrible in Word -- programmatically or otherwise. This is a great thing and great project.

@PrzemyslawKlys
Copy link
Member

New version (on github), not yet released to PS Gallery changes things a bit and affects how Add-WordTable works. It will now work in a way where you can expect $data | Format-Table to be identical in word. So if you have hashtable or ordinarydictionary you need to use -Transpose switch. There is also external command that can do that "change" before applying it to Word. Format-TransposeTable

@PrzemyslawKlys PrzemyslawKlys added the enhancement New feature or request label Aug 7, 2018
@ylepine
Copy link
Contributor

ylepine commented Mar 25, 2022

Actually, I'm creating documents from a template with all Styles for differents sections... It would be nice to be able to, at least, use existing styles when adding text... not really needs to create them in my case.

@PrzemyslawKlys
Copy link
Member

I am working on a new project which will be a total rewrite (not only on PowerShell level) but also on .NET level meaning PSWriteWord will be archived and the new project will take over. I will have full control over the .NET code so a lot of different options will be possible. It will have much larger options. I did a sneak peak on Twitter - https://twitter.com/PrzemyslawKlys/status/1496576889250582533

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants