kjambunathan / org-mode-ox-odt

The Authoritative fork of Org mode's ODT exporter
GNU General Public License v3.0
47 stars 9 forks source link

Feature request: use libreoffice table template #199

Open QiangF opened 2 years ago

QiangF commented 2 years ago

I am trying to export the attached snippet to get the table look like the attached odt file. I can't get it working.

It might be much easier to just open libreoffice, define a table template and include that file as a style file, rather than manually fix the table styles, especially border width, background color, these table style is much more clear in a libreoffice table template, just keep the width and alignment cookie if possible.

Then we can just specify the template name in the table's attr_odt.

snippet.txt

test.odt

QiangF commented 2 years ago

I have noticed libreoffice provide table-template keyword, and by default it has a table template named "Academic".

<style:style style:name="AcademicFirstRowTableCell" style:family="table-cell" style:data-style-name="N20100">
  <style:table-cell-properties fo:border-top="0.74pt solid #000000" fo:border-bottom="0.74pt solid #000000" fo:padding="0cm" fo:padding-left="0cm" fo:padding-right="0cm" fo:padding-top="0cm" fo:padding-bottom="0cm"/>
</style:style>
<style:style style:name="AcademicLastRowTableCell" style:family="table-cell" style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000" fo:padding="0cm" fo:padding-left="0cm" fo:padding-right="0cm" fo:padding-top="0cm" fo:padding-bottom="0cm"/>
</style:style>
<style:style style:name="AcademicFirstColumnTableCell" style:family="table-cell" style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm" fo:padding-left="0cm" fo:padding-right="0cm" fo:padding-top="0cm" fo:padding-bottom="0cm"/>
</style:style>
<style:style style:name="AcademicLastColumnTableCell" style:family="table-cell" style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm" fo:padding-left="0cm" fo:padding-right="0cm" fo:padding-top="0cm" fo:padding-bottom="0cm"/>
</style:style>

<table:table-template table:name="Academic" table:first-row-end-column="row"
                      table:first-row-start-column="row" table:last-row-end-column="row"
                      table:last-row-start-column="row">
  <table:first-row table:style-name="AcademicFirstRowTableCell"/>
  <table:last-row table:style-name="AcademicLastRowTableCell"/>
  <table:first-column table:style-name="AcademicFirstColumnTableCell"/>
  <table:last-column table:style-name="AcademicLastColumnTableCell"/>
</table:table-template>

Customising styles when you can see it is much more easier than eding xml file. Maybe we can put all styles used by this exporter in an odt file with examples.

kjambunathan commented 2 years ago

table:table-template

Table template started appearing in the stylist only in last 1-2 years. But it wasn't using the table-template ... If table-template started appearing, I can start using it. What version of LibreOffice are you using? Could you please spot the mention of this feature in release notes ....

I will look at it. All the infrastructure is in place. It is just a question of putting things together.

QiangF commented 2 years ago

I am not sure of when it is in use. I am using an old appimage, version is 7.3.1.3. The current stable is 7.3.4. It sure will make the code cleaner to use table-template. Current implementation of first row and last row is perhaps fragile.

kjambunathan commented 2 years ago

To get this

Screenshot from 2022-07-24 15-14-30

do this

#+NAME: revenue
#+CAPTION: Quarterly Revenue
#+ATTR_ODT: :style "Academic"
|       | Jan | Feb | Mar | Sum |
|-------+-----+-----+-----+-----|
| North | 5.1 | 1.7 | 5.6 |     |
| South | 7.6 | 3.7 | 0.4 |     |
| East  | 9.5 | 0.4 | 9.6 |     |
| West  | 4.8 | 5.7 | 5.9 |     |
|-------+-----+-----+-----+-----|
| Sum   |     |     |     |     |
#+TBLFM: $5=vsum($2..$4)::@6$2..@6$4=vsum(@I..@II)::@>$>=vsum(@I..@II)

#+ATTR_ODT: :target "automatic_styles"
#+begin_src nxml
<style:style style:name="AcademicTableFirstRowCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableLastRowCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableFirstColumnCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableLastColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableEvenRowCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableOddRowCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableEvenColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableOddColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableBodyCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableBackgroundCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.11"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.12"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.13"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.14"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.15"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.16"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<!-- <table:table-template table:first-row-end-column="row" -->
<!--                       table:first-row-start-column="row" -->
<!--                       table:last-row-end-column="row" -->
<!--                       table:last-row-start-column="row" -->
<!--                       table:name="Academic"> -->
<!--   <table:first-row table:style-name="AcademicTableFirstRowCell" /> -->
<!--   <table:last-row table:style-name="AcademicTableLastRowCell" /> -->
<!--   <table:first-column table:style-name="AcademicTableFirstColumnCell" /> -->
<!--   <table:last-column table:style-name="AcademicTableLastColumnCell" /> -->
<!--   <table:body table:style-name="AcademicTableBodyCell" /> -->
<!--   <table:even-rows table:style-name="AcademicTableEvenRowCell" /> -->
<!--   <table:odd-rows table:style-name="AcademicTableOddRowCell" /> -->
<!--   <table:even-columns table:style-name="AcademicTableEvenColumnCell" /> -->
<!--   <table:odd-columns table:style-name="AcademicTableOddColumnCell" /> -->
<!--   <table:background table:style-name="AcademicTableBackgroundCell" /> -->
<!-- </table:table-template> -->
#+end_src

# Local Variables:
# eval: (add-to-list 'org-odt-table-styles '("Academic" "Academic" ((use-first-row-styles . t) (use-last-row-styles . t) (use-banding-rows-styles . t) (use-banding-columns-styles . t))))
# org-odt-table-cell-variant-calculator: nil
# eval: (add-hook 'org-export-before-processing-hook (defun my-recalculte-tables-before-export (_) (org-table-recalculate-buffer-tables)))
# End:

This is the emacs-lisp code I used for migrating the LibreOffice defined table template, to a table template that ox-odt can use. This snippet is for my own reference.

  1. Open a LibreOffice produced odt file that uses table template.
  2. Unzip the odt file, and open styles.xml. Copy the <table template ...> ... </table:table-template> of interest to kill ring.
  3. With point still in styles.xml, do M-x rename-table-template.
  4. Copy the renamed tables for use as automatic style in org file.

(If the above description is vague use the clues in the emacs-lisp snippet an org file)

(defun rename-table-template ()
  (interactive)
  (let* (;; (s "
     ;; <table:table-template table:first-row-end-column=\"row\"
     ;;               table:first-row-start-column=\"row\"
     ;;               table:last-row-end-column=\"row\"
     ;;               table:last-row-start-column=\"row\"
     ;;               table:name=\"Simple Grid Rows\">
     ;;   <table:first-row table:style-name=\"Simple_20_Grid_20_Rows.1\" />
     ;;   <table:last-row table:style-name=\"Simple_20_Grid_20_Rows.2\" />
     ;;   <table:first-column table:style-name=\"Simple_20_Grid_20_Rows.3\" />
     ;;   <table:last-column table:style-name=\"Simple_20_Grid_20_Rows.4\" />
     ;;   <table:body table:style-name=\"Simple_20_Grid_20_Rows.9\" />
     ;;   <table:even-rows table:style-name=\"Simple_20_Grid_20_Rows.5\" />
     ;;   <table:odd-rows table:style-name=\"Simple_20_Grid_20_Rows.6\" />
     ;;   <table:even-columns table:style-name=\"Simple_20_Grid_20_Rows.7\" />
     ;;   <table:odd-columns table:style-name=\"Simple_20_Grid_20_Rows.8\" />
     ;;   <table:background table:style-name=\"Simple_20_Grid_20_Rows.10\" />
     ;; </table:table-template>")

     ;; (s (buffer-substring-no-properties (region-beginning) (region-end)))
     ;; (s1 (org-odt--xml-to-lisp s))

     (s (or (and kill-ring (current-kill 0)) ""))
     (s1 (org-odt--xml-to-lisp s))
     (template-name-1 (assoc-default 'table:name (nth 1 s1)))
     (template-name (replace-regexp-in-string " " "" template-name-1))
     (x s1)
     (x (->> (cddr x)
         (--map (cons (nth 0 it) (alist-get 'table:style-name (nth 1 it))))
         (--map (cons (cdr it)
                  (format "%s%s%s"
                      template-name
                      (->> (split-string (symbol-name (car it)) "[:-]")
                       (-map #'capitalize)
                       (--map (if (string-match-p (rx-to-string '(and "s" eos)) it)
                              (substring it 0 -1)
                            it))
                       (apply #'concat))
                      "Cell")))))
     (x (->> x (--map (cons (org-trim (car it))
                (org-trim (cdr it))))))
     (y (->> (-concat (list (cons template-name-1 template-name))
              x
              (->> x
                   (--map (cons (replace-regexp-in-string "_20_" " " (car it)) (cdr it)))))
         (--map (cons (format "\"%s\"" (car it))
                  (format "\"%s\"" (cdr it)))))))
    (goto-char (point-min))
    (while (and (not (eobp)) (re-search-forward (rx-to-string `(or ,@(mapcar #'car y)))))
      (let* ((m (match-string 0))
         (r (assoc-default (match-string 0) y)))
    (replace-match r t t)
    ;; (inspect (format "Replaced %s -> %s" m r))))
    ;; y
    ))
kjambunathan commented 2 years ago

Suppose I want this to be file local (setq org-odt-table-styles '("Academic" "Academic" ((use-first-row-styles . t) (use-last-row-styles . nil))))

Use the snippet below, and change value as needed.

# Local Variables:
# eval: (make-variable-buffer-local 'org-odt-table-styles)
# eval: (add-to-list 'org-odt-table-styles '("Academic1" "Academic1" ((use-first-row-styles . t) (use-last-row-styles . t) (use-banding-rows-styles . t) (use-banding-columns-styles . t))))
# org-odt-table-cell-variant-calculator: nil
# eval: (add-hook 'org-export-before-processing-hook (defun my-recalculte-tables-before-export (_) (org-table-recalculate-buffer-tables)))
# End:
QiangF commented 2 years ago

I am convinced now that the approach by this exporter is better. And you even ansered my question on stackexchange. I don't know we can put such a long line in the local variable block. Your emacs skill is way better than many of the masters I've met on the emacs china forum. Maybe you can share your emacs.d as many other skilled users do. The whole world will benifit. Thank you so much!

QiangF commented 2 years ago

Since the academic table style is used often, I want to put the stylde definition in "OrgOdtContentTemplate.xml", so I don't have to add the automatic style in every file. I put the following behind OrgTable, and it's not working.

<style:style style:name="Academic" style:family="table" style:parent-style-name="OrgTable">
  <style:table-properties style:rel-width="80%" table:align="center"/>
</style:style>

<style:style style:name="AcademicColumn" style:family="table-column" style:parent-style-name="OrgTableColumn">
  <style:table-column-properties style:rel-column-width="1*"/>
</style:style>

<style:style style:name="AcademicRow" style:family="table-row" style:parent-style-name="OrgTableRow">
  <style:table-row-properties fo:keep-together="always"/>
</style:style>

<style:style style:name="AcademicTableFirstRowCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableLastRowCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableFirstColumnCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableLastColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableEvenRowCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableOddRowCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableEvenColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableOddColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableBodyCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableBackgroundCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
QiangF commented 2 years ago

Can you please provide a most complete table template? I have also added the following:

kjambunathan commented 2 years ago

... and it's not working.

Share the snippet and show me a screenshot. This way I will know what is not working. Table styling is complex, and my gut feeling is that existing code needs minor adjustment.

I want both the org snippet and the odt file. A zip file is good ... Please don't add you own styles. It should be reproducible on my side.

QiangF commented 2 years ago

I have attached these files after pulling the latest change. Now the issue is with the alignment cookies. The alignment seems to be hard coded to "Custom" style. test2.zip

Maybe decouping of the current table style is easier to sort out, there are just too many table cell styles to define for a new template, (first/last row, first/last column, even/odd, left aligned/centered/right aligned).

The docstring says if I need to define a new table style, I don't need to define all cell styles.

You need to define the styles \"MyOrgTableContents\" and
\"MyOrgTableHeading\" as part of your styles file.  Rest of the 6
styles will be defined *automatically* for you."

But if the new style is named “OrgTable”, it won't work. ( I can not change "OrgTable" style to a smaller font" via changing the style.xml file, it's also hard coded in ox-odt.el )

QiangF commented 2 years ago

LibreOfficeTables.odt

LibreOffice comes with a handful of table template, it will be very helpful to include some of those in the default style file.

QiangF commented 1 year ago

The table style is still very complicated. For example, if I want the first row to have bold text. I add “AcademicFirstRowContents” style in this file

test.txt

The added style specification is:

<style:style style:name="AcademicFirstRowContents" style:family="paragraph" style:parent-style-name="Table_20_Contents">
  <loext:graphic-properties draw:fill="none" draw:fill-color="#729fcf"/>
  <style:paragraph-properties fo:margin-left="0cm"
                              fo:margin-right="0cm"
                              fo:margin-top="0cm"
                              fo:margin-bottom="0cm"
                              fo:text-indent="0cm"
                              fo:line-height="150%"
                              loext:contextual-spacing="false"
                              style:auto-text-indent="false"
                              style:page-number="auto"
                              fo:background-color="transparent"
                              style:shadow="none">
  </style:paragraph-properties>
  <style:text-properties fo:color="#800000"
                           fo:font-weight="bold"
                           style:font-weight-asian="bold"
                           style:font-weight-complex="bold"/>
</style:style>

The first line is not bold. Another thing that is counter-intuitive is the line below the first row, if that is specified in the "Academic" table template, why add the separator in the table. I mean this line:

|-------+-----+-----+-----+-----|

The above file is mostly copied from your reply:

do this

#+NAME: revenue
#+CAPTION: Quarterly Revenue
#+ATTR_ODT: :style "Academic"
|       | Jan | Feb | Mar | Sum |
|-------+-----+-----+-----+-----|
| North | 5.1 | 1.7 | 5.6 |     |
| South | 7.6 | 3.7 | 0.4 |     |
| East  | 9.5 | 0.4 | 9.6 |     |
| West  | 4.8 | 5.7 | 5.9 |     |
|-------+-----+-----+-----+-----|
| Sum   |     |     |     |     |
#+TBLFM: $5=vsum($2..$4)::@6$2..@6$4=vsum(@I..@II)::@>$>=vsum(@I..@II)

#+ATTR_ODT: :target "automatic_styles"
#+begin_src nxml
<style:style style:name="AcademicTableFirstRowCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableLastRowCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableFirstColumnCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableLastColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableEvenRowCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableOddRowCell"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableEvenColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableOddColumnCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableBodyCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="AcademicTableBackgroundCell"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.11"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.12"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.13"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.14"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.15"
             style:family="table-cell"
             style:data-style-name="N20100">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:border-top="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<style:style style:name="Academic.16"
             style:family="table-cell"
             style:data-style-name="N20000">
  <style:table-cell-properties fo:border-bottom="0.74pt solid #000000"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
</style:style>
<!-- <table:table-template table:first-row-end-column="row" -->
<!--                       table:first-row-start-column="row" -->
<!--                       table:last-row-end-column="row" -->
<!--                       table:last-row-start-column="row" -->
<!--                       table:name="Academic"> -->
<!--   <table:first-row table:style-name="AcademicTableFirstRowCell" /> -->
<!--   <table:last-row table:style-name="AcademicTableLastRowCell" /> -->
<!--   <table:first-column table:style-name="AcademicTableFirstColumnCell" /> -->
<!--   <table:last-column table:style-name="AcademicTableLastColumnCell" /> -->
<!--   <table:body table:style-name="AcademicTableBodyCell" /> -->
<!--   <table:even-rows table:style-name="AcademicTableEvenRowCell" /> -->
<!--   <table:odd-rows table:style-name="AcademicTableOddRowCell" /> -->
<!--   <table:even-columns table:style-name="AcademicTableEvenColumnCell" /> -->
<!--   <table:odd-columns table:style-name="AcademicTableOddColumnCell" /> -->
<!--   <table:background table:style-name="AcademicTableBackgroundCell" /> -->
<!-- </table:table-template> -->
#+end_src

# Local Variables:
# eval: (add-to-list 'org-odt-table-styles '("Academic" "Academic" ((use-first-row-styles . t) (use-last-row-styles . t) (use-banding-rows-styles . t) (use-banding-columns-styles . t))))
# org-odt-table-cell-variant-calculator: nil
# eval: (add-hook 'org-export-before-processing-hook (defun my-recalculte-tables-before-export (_) (org-table-recalculate-buffer-tables)))
# End:
QiangF commented 1 year ago

The current way of doing customisation looks very messy, user have to put boilerplate text like #+odt_automatic_styles: and #+odt_extra_styles:, especially for table styles.

A much cleaner way to to maintain a central style file collection, and include those files in the document.

For example, for the table style "FirstBold", I want to just add one line to the file that uses it:

+include: ./table_FirstBold.org

table_FirstBold.org.txt

That way, sperating style definition from the content give user a cleaner work space to get focused.

kjambunathan commented 1 year ago

+include: ./table_FirstBold.org table_FirstBold.org.txt

That way, sperating style definition from the content give user a cleaner work space to get focused.

You can use SETUPFILE for that

'#+SETUPFILE: file'

The setup file or a URL pointing to such file is for additional in-buffer settings. Org loads this file and parses it for any settings in it only when Org opens the main file. If URL is specified, the contents are downloaded and stored in a temporary file cache. [C-c C-c]{.kbd} on the settings line parses and loads the file, and also resets the temporary file cache. Org also parses and loads the document during normal exporting process. Org parses the contents of this document as if it was included in the buffer. It can be another Org file. To visit the file---not a URL---use [C-c \']{.kbd} while point is on the line with the file name.

In-buffer settings may appear anywhere in the file, either directly or indirectly through a file included using '`#+SETUPFILE: filename or URL syntax.

kjambunathan commented 1 year ago

You can use SETUPFILE for that

The nxml blocks will appear in the output if you are exporting to formats other than ODT. In that case you can use per-backend SETUP file. See Mix Starmath and LaTeX / Mix English and Tamil in same Org file (or) How to conditionally export text using Macros and Drawers in Org mode

QiangF commented 1 year ago

I have never got the table template working.

  1. I got the "Box List Red" table template from the standard libreoffice table template list.

  2. I extracted the "Box List Red" template and put it in this file. box_list_red.org.txt

  3. After I export, the table looks like this img_2023_09_19__17_19_06

If I manually apply the "Box List Red" table style by put cursor inside the table and double click on the table template, I get this img_2023_09_19__17_19_46

The corresponding odt file is box_list_red.org.odt.txt

kjambunathan commented 1 year ago

Zip file

issue-199-boxlistred.zip

Screenshot

Screenshot-2023-09-19_20-56

Org snippet

# #+odt_prettify_xml: tidy+indent

#+begin_src emacs-lisp :exports results :results none
  (add-to-list 'org-odt-table-styles
           '("Academic" "Academic"
         ((use-first-row-styles . t)
          (use-last-row-styles . t)
          (use-banding-rows-styles . t)
          (use-banding-columns-styles . t)
          )))
  (setq org-odt-table-cell-variant-calculator nil)
  (add-hook 'org-export-before-processing-hook
        (defun my-recalculte-tables-before-export (_)
          (org-table-recalculate-buffer-tables)))
#+end_src

#+NAME: revenue
#+CAPTION: Quarterly Revenue
#+ATTR_ODT: :style "BoxListRed"
|       | Jan | Feb | Mar | Sum |
|-------+-----+-----+-----+-----|
| North | 5.1 | 1.7 | 5.6 |     |
| South | 7.6 | 3.7 | 0.4 |     |
| East  | 9.5 | 0.4 | 9.6 |     |
| West  | 4.8 | 5.7 | 5.9 |     |
|-------+-----+-----+-----+-----|
| Sum   |     |     |     |     |
#+TBLFM: $5=vsum($2..$4)::@6$2..@6$4=vsum(@I..@II)::@>$>=vsum(@I..@II)

#+ATTR_ODT: :target "automatic_styles"
#+begin_src nxml
<table:table-template table:first-row-end-column="row"
                      table:first-row-start-column="row"
                      table:last-row-end-column="row"
                      table:last-row-start-column="row"
                      table:name="BoxListRed">
  <table:first-row table:style-name="BoxListRedTableFirstRowCell" />
  <table:last-row table:style-name="BoxListRedTableLastRowCell" />
  <table:first-column table:style-name="BoxListRedTableFirstColumnCell" />
  <table:last-column table:style-name="BoxListRedTableLastColumnCell" />
  <table:body table:style-name="BoxListRedTableBodyCell" />
  <table:even-rows table:style-name="BoxListRedTableEvenRowCell" />
  <table:odd-rows table:style-name="BoxListRedTableOddRowCell" />
  <table:even-columns table:style-name="BoxListRedTableEvenColumnCell" />
  <table:odd-columns table:style-name="BoxListRedTableOddColumnCell" />
  <table:background table:style-name="BoxListRedTableBackgroundCell" />
</table:table-template>
<style:style style:name="BoxListRedTableFirstRowCell"
             style:display-name="BoxListRedTableFirstRowCell"
             style:family="table-cell"
             style:data-style-name="N30100">
  <style:table-cell-properties fo:background-color="#ce181e"
                               fo:border-top="0.74pt solid #ce181e"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:color="#ffffff"
             fo:font-family="'Liberation Sans'"
             fo:font-weight="bold"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss"
             style:font-weight-asian="bold"
             style:font-weight-complex="bold" />
</style:style>
<style:style style:name="BoxListRedTableLastRowCell"
             style:display-name="BoxListRedTableLastRowCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:background-color="#fcd3c1"
                               fo:border-bottom="0.74pt solid #ce181e"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             fo:font-weight="bold"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss"
             style:font-weight-asian="bold"
             style:font-weight-complex="bold" />
</style:style>
<style:style style:name="BoxListRedTableFirstColumnCell"
             style:display-name="BoxListRedTableFirstColumnCell"
             style:family="table-cell"
             style:data-style-name="N30100">
  <style:table-cell-properties fo:border-left="0.74pt solid #ce181e"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableLastColumnCell"
             style:display-name="BoxListRedTableLastColumnCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:border-right="0.74pt solid #ce181e"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableBodyCell"
             style:display-name="BoxListRedTableBodyCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:background-color="#dddddd"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableEvenRowCell"
             style:display-name="BoxListRedTableEvenRowCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableOddRowCell"
             style:display-name="BoxListRedTableOddRowCell"
             style:family="table-cell"
             style:data-style-name="N30100">
  <style:table-cell-properties fo:background-color="#dddddd"
                               fo:border-left="0.74pt solid #ce181e"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableEvenColumnCell"
             style:display-name="BoxListRedTableEvenColumnCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableOddColumnCell"
             style:display-name="BoxListRedTableOddColumnCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:background-color="#dddddd"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
<style:style style:name="BoxListRedTableBackgroundCell"
             style:display-name="BoxListRedTableBackgroundCell"
             style:family="table-cell"
             style:data-style-name="N30000">
  <style:table-cell-properties fo:background-color="#dddddd"
                               fo:border-right="0.74pt solid #ce181e"
                               fo:padding="0cm"
                               fo:padding-bottom="0cm"
                               fo:padding-left="0cm"
                               fo:padding-right="0cm"
                               fo:padding-top="0cm" />
  <style:text-properties style:font-name="Liberation Sans"
             style:font-name-asian="DejaVu Sans"
             style:font-name-complex="DejaVu Sans"
             fo:font-family="'Liberation Sans'"
             style:font-family-asian="'DejaVu Sans'"
             style:font-family-complex="'DejaVu Sans'"
             style:font-family-generic="swiss" />
</style:style>
#+end_src

Comments

Table cells doesn't support inheritance. This was the case, some 10 or so years ago, when I implemented the table styles.

Table templates as defined by ODF standard isn't very helpful--they are too complex and the extent of its application is too limited. They are also too complex to create.

While working on your question, I discovered some things for the first time. See comment in the lisp snippet below


(table:table-template
   ((table:name . "Box List Red")
    ;; The 4 attributes below this comment aren't handled by the ODT
    ;; exporter.  (I am seeing these attributes for the first time)
    (table:first-row-end-column . "row")
    (table:first-row-start-column . "row")
    (table:last-row-end-column . "row")
    (table:last-row-start-column . "row"))
   (table:first-row ((table:style-name . "Box_20_List_20_Red.1")))
   (table:last-row ((table:style-name . "Box_20_List_20_Red.2")))
   (table:first-column ((table:style-name . "Box_20_List_20_Red.3")))
   (table:last-column ((table:style-name . "Box_20_List_20_Red.4")))

   (table:even-rows ((table:style-name . "Box_20_List_20_Red.5")))
   (table:odd-rows ((table:style-name . "Box_20_List_20_Red.6")))
   (table:even-columns ((table:style-name . "Box_20_List_20_Red.7")))
   (table:odd-columns ((table:style-name . "Box_20_List_20_Red.8")))
   ;; The 2 nodes below this comment aren't handled by the ODT
   ;; exporter.  (I am seeing these attributes for the first time)
   (table:body ((table:style-name . "Box_20_List_20_Red.9")))
   (table:background ((table:style-name . "Box_20_List_20_Red.10"))))

This is how the table template in https://github.com/kjambunathan/org-mode-ox-odt/files/12658084/box_list_red.org.odt.txt looks like, and it has lo:ext . See the comments in lisp for details.


(table:table-template
 ((table:name . "Box List Red") (table:first-row-end-column . "row")
  (table:first-row-start-column . "row")
  (table:last-row-end-column . "row")
  (table:last-row-start-column . "row"))
 (table:first-row ((table:style-name . "Box_20_List_20_Red.1")))
 (table:last-row ((table:style-name . "Box_20_List_20_Red.2")))
 (table:first-column ((table:style-name . "Box_20_List_20_Red.3")))
 (table:last-column ((table:style-name . "Box_20_List_20_Red.4")))
 (table:body ((table:style-name . "Box_20_List_20_Red.9")))
 (table:even-rows ((table:style-name . "Box_20_List_20_Red.5")))
 (table:odd-rows ((table:style-name . "Box_20_List_20_Red.6")))
 (table:even-columns ((table:style-name . "Box_20_List_20_Red.7")))
 (table:odd-columns ((table:style-name . "Box_20_List_20_Red.8")))
 (table:background ((table:style-name . "Box_20_List_20_Red.10")))
 ;; `loext:' elements are non-standard LO-specific items.  It is
 ;; better to avoid producing documents that are in "extended" format.
 ;; Always save documents in `ODF-v1.2' complaint format.  (These days
 ;; I am seeing `ODF-v1.3', but it is better to stick with `ODF-v1.2'
 ;; if you want to compatibility with `Google Docs' or `docx' formats.
 ;; We have already discovered that `Google Docs' and 'docx' have
 ;; difficult rendering standard complaint ODF-v1.2 documents.)
 (loext:first-row-even-column
  ((table:style-name . "Box_20_List_20_Red.15")))
 (loext:last-row-even-column
  ((table:style-name . "Box_20_List_20_Red.16")))
 (loext:first-row-end-column
  ((table:style-name . "Box_20_List_20_Red.12")))
 (loext:first-row-start-column
  ((table:style-name . "Box_20_List_20_Red.11")))
 (loext:last-row-end-column
  ((table:style-name . "Box_20_List_20_Red.14")))
 (loext:last-row-start-column
  ((table:style-name . "Box_20_List_20_Red.13"))))

LO-Settings-2023-09-19_21-27

As you already noted, the snippet I suggested doesn't produce table cell variants for vertical, horizontal alignment, borders etc.

There are more remarks I can add, but it won't be of interest to you.

kjambunathan commented 1 year ago

I have never got the table template working.

You need to "rewrite" the table template using the rename-table-template snippet shared in https://github.com/kjambunathan/org-mode-ox-odt/issues/199#issuecomment-1193283903. (I just noticed that the rename-table-template has missing trailing closing parens. Once you close the defun evals fine and just works)

I am also working on ways to improve the table template infrastructure, not because we want better tables, but because they are a good ways to improve the dom-based tools that I am working on. They are WIP. Once I am happy with dom API tools, I will add an easier way to "import" LO table templates in a manner that the exporter can use.

The lisp snippets below are for my own reference. They use APIs that are NOT available in the repo.

I used these helpers to generate the Box List Red table template that I shared in the org snippet above. It is a glorified version of rename-table-template that I shared previously.



;;; A better version of `rename-table-template' 

(pcase-let* ((dom (with-current-buffer (find-file-noselect "/home/kjambunathan/tmp/odt/issue-199/boxlistred/styles.xml")
                    (odt-dom:file->dom (buffer-file-name) nil)))
             (`(,table-template ,table-template-name)
              (car (odt-dom--step dom %//table:table-template t (list %_self %table:name))))
             (new-table-template-name (->> table-template-name
                                           (s-replace " " ""))))
  (with-current-buffer (get-buffer-create (format "%s.xml" new-table-template-name))
    (erase-buffer)
    (pop-to-buffer (current-buffer))
    (nxml-mode)
    (save-excursion
      (map-put! (odt-dom-properties table-template) 'table:name new-table-template-name)
      (->> (odt-dom--step table-template
                          %_children t (cons %table:style-name
                                             (-> %_tag
                                                 symbol-name
                                                 (split-string ":")
                                                 cdr
                                                 car
                                                 (split-string "-")
                                                 (->> (-map #'capitalize))
                                                 (string-join "")
                                                 (->> (funcall (lambda (it)
                                                                 (if (string-match-p (rx-to-string '(and "s" eos)) it)
                                                                     (substring it 0 -1)
                                                                   it))))
                                                 (->> (funcall (-cut concat (->> table-template-name
                                                                                 (replace-regexp-in-string " " ""))
                                                                     "Table" <> "Cell"))))))
           (seq-map (pcase-lambda (`(,old . ,new))
                      (->> (odt-dom--step dom %_all (string= %table:style-name old)
                                          (prog1 %_self
                                            (->> '(table:style-name)
                                                 (seq-map
                                                  (lambda (it)
                                                    (map-put! %_self%_properties it new)))))))
                      (->> (odt-dom--step dom %_all (string= %style:name old)
                                          (prog1 %_self
                                            (->> '(style:name style:display-name)
                                                 (seq-map
                                                  (lambda (it)
                                                    (map-put %_self%_properties it new))))))
                           car)))
           (cons table-template)
           (--map (org-odt--lisp-to-xml it nil t))
           (s-join "\n")
           insert))))

;; Obtain list of table templates
(let* ((dom (with-current-buffer (find-file-noselect "/home/kjambunathan/tmp/odt/issue-199/boxlistred/styles.xml")
              (odt-dom:file->dom (buffer-file-name) ;; nil
                                 ))))
  (->> (odt-dom--step dom %//table:table-template t %_self)
       (-map (lambda (table-template)
               (let* (;; (table-template (car (odt-dom--step dom %//table:table-template t %_self)))
                      (table-template-name (odt-dom-property table-template 'table:name)))
                 (->> (odt-dom--step table-template
                                     %_children t %table:style-name)
                      (seq-map (lambda (old)
                                 (->> (odt-dom--step dom %_all (string= %style:name old)
                                                     %_self)
                                      car)))
                      (cons table-template)
                      (cons table-template-name)))))))

;;; `rename-table-template' with missing closing parens added

(defun rename-table-template ()
  (interactive)
  (let* (;; (s "
         ;; <table:table-template table:first-row-end-column=\"row\"
         ;;                       table:first-row-start-column=\"row\"
         ;;                       table:last-row-end-column=\"row\"
         ;;                       table:last-row-start-column=\"row\"
         ;;                       table:name=\"Simple Grid Rows\">
         ;;   <table:first-row table:style-name=\"Simple_20_Grid_20_Rows.1\" />
         ;;   <table:last-row table:style-name=\"Simple_20_Grid_20_Rows.2\" />
         ;;   <table:first-column table:style-name=\"Simple_20_Grid_20_Rows.3\" />
         ;;   <table:last-column table:style-name=\"Simple_20_Grid_20_Rows.4\" />
         ;;   <table:body table:style-name=\"Simple_20_Grid_20_Rows.9\" />
         ;;   <table:even-rows table:style-name=\"Simple_20_Grid_20_Rows.5\" />
         ;;   <table:odd-rows table:style-name=\"Simple_20_Grid_20_Rows.6\" />
         ;;   <table:even-columns table:style-name=\"Simple_20_Grid_20_Rows.7\" />
         ;;   <table:odd-columns table:style-name=\"Simple_20_Grid_20_Rows.8\" />
         ;;   <table:background table:style-name=\"Simple_20_Grid_20_Rows.10\" />
         ;; </table:table-template>")

         ;; (s (buffer-substring-no-properties (region-beginning) (region-end)))
         ;; (s1 (org-odt--xml-to-lisp s))

         (s (or (and kill-ring (current-kill 0)) ""))
         (s1 ;; (org-odt--xml-to-lisp s)
          (odt-xml-string-to-dom s))
         (template-name-1 (assoc-default 'table:name (nth 1 s1)))
         (template-name (replace-regexp-in-string " " "" template-name-1))
         (x s1)
         (x (->> (cddr x)
                 (--map (cons (nth 0 it) (alist-get 'table:style-name (nth 1 it))))
                 (--map (cons (cdr it)
                              (format "%s%s%s"
                                      template-name
                                      (->> (split-string (symbol-name (car it)) "[:-]")
                                           (-map #'capitalize)
                                           (--map (if (string-match-p (rx-to-string '(and "s" eos)) it)
                                                      (substring it 0 -1)
                                                    it))
                                           (apply #'concat))
                                      "Cell")))))
         (x (->> x (--map (cons (org-trim (car it))
                                (org-trim (cdr it))))))
         (y (->> (-concat (list (cons template-name-1 template-name))
                          x
                          (->> x
                               (--map (cons (replace-regexp-in-string "_20_" " " (car it)) (cdr it)))))
                 (--map (cons (format "\"%s\"" (car it))
                              (format "\"%s\"" (cdr it)))))))
    (goto-char (point-min))
    (while (and (not (eobp)) (re-search-forward (rx-to-string `(or ,@(mapcar #'car y)))))
      (let* ((m (match-string 0))
             (r (assoc-default (match-string 0) y)))
        (replace-match r t t)
        ;; (inspect (format "Replaced %s -> %s" m r))))
        ;; y
        ))))
kjambunathan commented 1 year ago

https://github.com/kjambunathan/org-mode-ox-odt/files/12658084/box_list_red.org.odt.txt

I have never got the table template working.

You need to "rewrite" the table template using the rename-table-template snippet shared in #199 (comment). (I just noticed that the rename-table-template has missing trailing closing parens. Once you close the defun evals fine and just works)

May be may be ... I can make directly make use of the table template definition without resorting to renaming of table styles. (This renaming of style is necessary because ODT expects that styles follow certain "syntax". May be if I relax this "strict naming convention" rule ... things could be simpler)

Let me see .... These things are doable but mechanics are little bit boring. So, expect that things move slowly on table side. There are lots of table related changes on my git stash which I do NOT feel like looking again ... So thing will move very slowly, but it would definitely move.

kjambunathan commented 1 year ago

I was reviewing my response on myobile. Noticed a minor copy-paste error

In the "Box List Red" org snippet, change "Academic" to "BoxListRed". Once you do that, you will see a result similar to my screenshot

QiangF commented 1 year ago

It works! Importing table template from libreoffice is not that important, direct change the xml file is much easier, once you provide some examples like the one for this issue. Programmatically define an ad-hoc style/template from the improved dom-based tools will be nice.

I also find the "#+include: " keyword works seamlessly with my central style folder scheme, I can put all my style definition in one folder.

The modified file according to your last reply is here: table_boxlistred.org.txt test.org.txt

QiangF commented 1 year ago

One last problem, the cell alignment is broken. img_2023_09_20__12_08_03

test.org.txt table_FirstRowBold.org.txt

kjambunathan commented 1 year ago

May be may be ... I can make directly make use of the table template definition without resorting to renaming of table styles.

This doesn't work. See LO Bug 157350: Table ODF Table Template is not honored (in LO 24.2 daily builds) (In other words, non-compliance with ODF-v1.2 standard) -

kjambunathan commented 1 year ago

Re-opened the bug ... to keep track of the work in progress.