-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Description
First of all, @scanny, thank you very much for all your work for python-docx. A lot of your answers here and on SO have been very helpful.
I wish to either replace existing text or add new text to each cell in a table without altering the font properties that have already been set for the cell paragraph. I have used the following approach to set the text at the run level (rather than at the cell/paragraph level).
for row in table.rows:
for cell in table.cells:
paragraph = cell.paragraphs[0]
run = paragraph.add_run()
run.text = my_text
This results in the following xml for each cell.
document.xml
<w:tc>
<w:tcPr>
<w:tcW
w:w="4464"
w:type="dxa" />
</w:tcPr>
<w:p
w:rsidR="00494DA3"
w:rsidRPr="006C725A"
w:rsidRDefault="00494DA3"
w:rsidP="00990A0D">
<w:pPr>
<w:jc w:val="left" />
<w:rPr>
<w:rFonts
w:ascii="MS 明朝"
w:hAnsiTheme="minorEastAsia"
w:cs="Courier New" />
</w:rPr>
</w:pPr>
<w:r>
<w:t>My text</w:t>
</w:r>
</w:p>
</w:tc>
Looking at the above xml, as far as I understand, due to the <w:rPr> tag, ascii text should be shown in the "MS 明朝" (MS Mincho) font, which is what I want. However, when the docx file is opened, the ascii text ("My text" here) is actually being shown in the "Courier New" font.
My guess is that the ascii font is being taken from the default Normal paragraph font given in style.xml shown below.
style.xml
<w:style
w:type="paragraph"
w:default="1"
w:styleId="a">
<w:name w:val="Normal" />
<w:qFormat />
<w:pPr>
<w:widowControl w:val="0" />
<w:jc w:val="both" />
</w:pPr>
<w:rPr>
<w:rFonts
w:ascii="Courier New"
w:hAnsi="Courier New" />
<w:kern w:val="2" />
<w:sz w:val="24" />
</w:rPr>
</w:style>
Do you have any ideas on what I should do here to add text to a paragraph so that the text is shown in the ascii font specified in that paragraph?
For example, when using add_run(), is it possible to somehow add the same <w:rPr> tag shown in each paragraph in document.xml to the new run using python-docx? For example:
<w:r>
<w:rPr>
<w:rFonts
w:ascii="MS 明朝"
w:hAnsiTheme="minorEastAsia"
w:cs="Courier New" />
</w:rPr>
<w:t>My text</w:t>
</w:r>
Any help/advice would be greatly appreciated.
Thanks in advance!