Search code examples
pythonpython-docx

Use Python docx to create phonetic guide / 'Ruby text' in Word?


I want to add phonetic guides to words in MS Word. Inside of MS Word the original word is called 'Base text' and the phonetic guide is called 'Ruby text.'

Here's what I'm trying to create looks like in Word: MS Word Phonetic Guide

The docx documentation has page that talks about Run-level content with a reference to ruby: <xsd:element name="ruby" type="CT_Ruby"/> located here: https://python-docx.readthedocs.io/en/latest/dev/analysis/features/text/run-content.html

I can not figure out how to access these in my code.

Here's an example of one of my attempts:

import docx
from docx import Document
document = Document()
base_text = '光栄'
ruby_text = 'こうえい'
p = document.add_paragraph(base_text)
p.add_run(ruby_text).ruby = True
document.save('ruby.docx')

But this code only returns the following:

光栄こうえい

I've tried to use ruby on the paragraph and p.text, removing the = True but I keep getting the error message 'X object has no attribute 'ruby'

Can someone please show me how to accomplish this?


Solution

  • The <xsd:element name="ruby" ... excerpt you mention is from the XML Schema type for a run. This means a child element of type CT_Ruby can be present on a run element (<w:r>) with the tag name <w:ruby>.

    There is not yet any API support for this element in python-docx, so if you want to use it you'll need to manipulate the XML using low-level lxml calls. You can get access to the run element on run._r. If you search on "python-docx workaround function" and also perhaps "python-pptx workaround function" you'll find some examples of doing this to extend the functionality.