Skip to content

Captions that behave like captions #676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ppebay opened this issue May 17, 2019 · 4 comments
Open

Captions that behave like captions #676

ppebay opened this issue May 17, 2019 · 4 comments

Comments

@ppebay
Copy link

ppebay commented May 17, 2019

Say that I appended to document an inline picture contained in a file called figure_file_name as follows:
add_picture(figure_file_name)
Then, say that I am subsequently creating a caption as follows:

p = document.paragraphs[-1]
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
caption = document.add_paragraph("Text of the caption goes here", style='Caption')
caption.alignment = WD_ALIGN_PARAGRAPH.CENTER

How do I then make this paragraph, which has the Caption style indeed, actually behave like a caption, i.e., move with the figure (and possibly refer to it with a Fig. 1 marking), as if it had been created in the MS Word GUI?

If I am not missing anything, the API currently does not allow for it. It would be a nice feature to have however. Thank you.

@gister9000
Copy link

+1

@jaymegordo
Copy link

jaymegordo commented Mar 28, 2021

The method described in #359 works for adding captions with updating numbers as you'd expect.

Only catch is that the user has to cmd+A, F9 to update the doc caption numbers when first opened.

@buhtz
Copy link

buhtz commented Dec 2, 2022

Only catch is that the user has to cmd+A, F9 to update the doc caption numbers when first opened.

But why is it that way? After CMD+A F9 and saving the docx with MS Word the numbering is still there when reopen with Word.
So Word must save the numbering somehow.

The CMD+A F9 should be able via python3-docx, too.

@buhtz
Copy link

buhtz commented Dec 6, 2022

I found a code snippet about how to auto-update a TOC.

fldChar3 = OxmlElement('w:updateFields')
fldChar3.set(qn('w:val'), 'true')

Can this be used somehow to update the numbering fields in table/figure captions?

My current code to add captions to tables (and later figures) is this

    def _caption(self, table: docx.table.Table, caption: str):
        """
        Based on: https://github.com/python-openxml/python-docx/issues/359
        """
        target = 'Table'

        # caption type
        paragraph = self._doc.add_paragraph(f'{target} ', style='Caption')

        # numbering field
        run = paragraph.add_run()

        fldChar = docx.oxml.OxmlElement('w:fldChar')
        fldChar.set(docx.oxml.ns.qn('w:fldCharType'), 'begin')
        run._r.append(fldChar)

        instrText = docx.oxml.OxmlElement('w:instrText')
        instrText.text = f' SEQ {target} \\* ARABIC'
        run._r.append(instrText)

        fldChar = docx.oxml.OxmlElement('w:fldChar')
        fldChar.set(docx.oxml.ns.qn('w:fldCharType'), 'end')
        run._r.append(fldChar)

        # caption text
        paragraph.add_run(f' {caption}')

But I don't know how and where to add this auto-update element in there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants