[Python] Table of Contents of List of Figures and Tables are not clickable in PDF while...

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I am using pythn-docx(https://github.com/python-openxml/python-docx) to generate a docx file and then converting this docx file into PDF format using unoconv (https://github.com/unoconv/unoconv), everything working fine except Table of Contents of List of Figures and Tables in PDF, lists are appearing correct but it's not clickable.

    Approach to generate word file: Manually create a doc file in MS word with captions on Tables and pictures and then manually created TOC of of List of Figures and Tables, then extracted the XML from this word file and find the xml of captions and Tables, Figures. and same XML used to generate docx in python. I am using from docx.oxml.shared import OxmlElement to generate xml which understand Microsoft word and MS Word figures that out when you open the document and it automatically fill the data based XML appended.

    Approach to covert docx to pdf: I am using unoconv python package to covert docx to pdf, unoconv is using libreoffice to convert docx to pdf

    Note: I have opened the same doc file in MS word and LibreOffice word, in both software TOC of List of Figures and Tables are clickable and able to navigate to the page on click. Ideally it should work in pdf as well but not working.

    Steps to generate TOC of List of Figures and Tables: Creating caption for tables and images in python-docx, caption xml:

    <w:p>
    <w:pPr>
    <w:pStyle w:val="Caption"/>
    </w:pPr>
    <w:r>
    <w:t xml:space="preserve">Table </w:t>
    </w:r>
    <w:r>
    <w:fldChar w:fldCharType="begin"/>
    <w:instrText> SEQ Table \* ARABIC</w:instrText> # for figure: SEQ Figure \* ARABIC
    <w:fldChar w:fldCharType="end"/>
    </w:r>
    <w:r>
    <w:t xml:space="preserve"> Test </w:t>
    </w:r>
    </w:p>


    and then creating List of Figures and Tables using XML:

    Table:

    <w:p>
    <w:r>
    <w:fldChar w:fldCharType="begin"/>
    </w:r>
    <w:r>
    <w:instrText xml:space="preserve">TOC \h \z \c "Table"</w:instrText>
    </w:r>
    <w:r>
    <w:fldChar w:fldCharType="separate"/>
    </w:r>
    <w:r>
    <w:t>Right-click to update field</w:t>
    </w:r>
    <w:r>
    <w:fldChar w:fldCharType="end"/>
    </w:r>
    </w:p>


    Figures:

    <w:p>
    <w:r>
    <w:fldChar w:fldCharType="begin"/>
    </w:r>
    <w:r>
    <w:instrText xml:space="preserve">TOC \h \z \c "Figure"</w:instrText>
    </w:r>
    <w:r>
    <w:fldChar w:fldCharType="separate"/>
    </w:r>
    <w:r>
    <w:t>Right-click to update field</w:t>
    </w:r>
    <w:r>
    <w:fldChar w:fldCharType="end"/>
    </w:r>
    </w:p>


    I think i am missing something in xml that libreoffice required to make clickable in pdf, help really appreciable, thank you in advance.

    Continue reading...

Compartilhe esta Página