PumasAI / WriteDocx.jl

A Julia package to create docx files for Microsoft Word from scratch
MIT License
21 stars 3 forks source link

Add individual styles to Tables #18

Open hyperkomplexe opened 2 months ago

hyperkomplexe commented 2 months ago

Enhancement - it would be great to be able to apply individual Table styles.

Similar to what you can do in the python-docx package. https://www.geeksforgeeks.org/working-with-tables-python-docx-module/ https://python-docx.readthedocs.io/en/latest/user/styles-using.html

For example: <w:tblStyle w:val="TableGridLight"/>

jkrumbiegel commented 2 months ago

Have you tried adding a style with table properties? I don't remember but this might already work to some degree. At least Style is parametric on the properties type, and this code here suggests I was using table styles at some point (I don't remember in what way though) https://github.com/PumasAI/WriteDocx.jl/blob/ba77b91617ce3fcf88cf72231db431ae23b7dfca/src/WriteDocx.jl#L1847

hyperkomplexe commented 2 months ago

Interesting, I gave it a quick try but it didn't change anything. It looks like the tblStyle tag doesn't get written into the docx file.

I need to look into it more, but I think it shouldn't be too hard just to pass in a string into TableProperties, which would bypass all of the cell borders, spacing etc elements.

Adding a style in the docx file seems pretty simple, it looks like just adding a tblStyle tag near the top of the file.

I tried it, but for some reason I can't zip the folder back together to create the docx file.

I'll play around a little, and see if I can get it working.

<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing">
  <w:body>
    <w:tbl>
      <w:tblPr>
        <w:tblCellSpacing w:w="10"/>
        <w:tblStyle w:val="TableGrid"/>
      </w:tblPr>
      <w:tr>
        <w:trPr/>
        <w:tc>
          <w:tcPr/>
jkrumbiegel commented 2 months ago

The zip file cannot have a top level folder, but standard tools usually add that. You can check in the WriteDocx code how ZipFiles.jl is used for that

jkrumbiegel commented 2 months ago

And I'll have to make a test case for the table styles, I don't think I had one.

hyperkomplexe commented 2 months ago

Ahh ok thanks, yeah just zipping a folder higher works.

Just adding that one line <w:tblStyle w:val="TableGrid"/> works, but I also have to copy the 'styles' file from an existing word file. I don't know if anything in the word file is proprietary to Microsoft, but I guess a work around would just be to add a few standard table styles to the WriteDocx package.

I'll play around, maybe I can make a pull request.