You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Paragaph.style attribute allows a paragraph style to be applied. The font typeface, size, and color can all be specified in a paragraph style.
There is not yet a way to apply a character style to a run. That will be an important and useful feature.
There is also not yet a direct way to manipulate a style in python-docx. That may also turn out to be an important feature.
A way to apply character formatting directly to a run will probably be last of these to come, since the effect can be achieved by defining a character style and applying it.
thanks for the feedback, so does it mean we can not set a paragraph to this specific example for current ? font(eg:Calibri) size(eg:10.5) color(eg red).
BTW: I am not clear about the meanning of "a run", can you give some explanations?
A run is a sequence of characters within a paragraph that share the same character formatting. So a single bold word in a paragraph would be in a separate run. That paragraph would need three runs, containing the text before the bold word, the text of the bold word, and the text after the bold word respectively.
You should be able to achieve the effect you're describing by creating a style in the "template" document you're using that has the font characteristics you want and then applying that paragraph style to the paragraph.
@vrvolle In the XML, applying a style to a run (a character style) looks like this:
<w:r>
<w:rPr>
<w:rStylew:val="Emphasis"/>
</w:rPr>
<w:t>words with character style applied</w:t>
</w:r>
If you're willing to go with a workaround until this can be implemented, something like this should do the trick:
fromdocx.oxml.sharedimportOxmlElement, qnrun=paragraph.add_run('words to emphasize')
rPr=run._r.get_or_add_rPr()
rStyle=OxmlElement('w:rStyle')
rStyle.set(qn('w:val'), 'Emphasis')
rPr.insert(0, rStyle)
I haven't tested this, so let me know if it gives you trouble and I can help you troubleshoot.
You'll need to discover the actual character style names by looking them up in the styles.xml of the document or by guessing. They should match the GUI names for the styles, but with spaces collapsed and capitalization possibly different.
An easy way to browse the styles.xml is by using opc-diag.
It is already working. I just copied the implementation for pStyle (though I didn't like the code duplication and having to work through "Run", "CT_R" and "CT_RPr" -- but as I said, I am very new to Python). What I really don't understand: why in one place there is
pStyle.get(qn('w:val'))
to access the value and
self.pStyle.val
at another.
Still missing: tests and documentation (try to finish until end of week).
Yep, that former one could be changed to look like the latter. Probably just an artifact of coding sequence. I've been a little loose with the .oxml subpackages because most of that code will be replaced by declarations that drive a configurable metaclass, once I get caught up a little further and can go heads down on that architectural bit :)
Looks great so far, never mind the seeming duplication, most or all of that part will get swallowed up by the metaprogramming once I'm done :)
Happy to have you work on other features. Best to start with determining the API. I use a page like this one in the developer documentation to get that sorted out: http://python-docx.readthedocs.org/en/latest/dev/analysis/features/breaks.html. Once you have a proposal there we can discuss and resolve. Then I start with a failing acceptance test. Both of these are separately committable steps even if there's no implementation.
Some folks like to spike then, to get the implementation working. But ultimately the work product that's the least work for me to commit (read: fastest time from pull request to commit) is working outside in, one test at a time. Here's a fairly typical example, the four commits starting with this one for adding the .bold property to a run: d04bbd0.
On the metaprogramming bit, what I had planned to do was add methods to a given class for the particular child elements and attributes it had, based on a declarative definition in the class. Roughly like what SQLAlchemy or Django does. So it would be something like:
The properties and methods would include ones for each child element and attribute, so it would get to be something of a long list. Implementations would generally be with standard closures define in or around the MetaClass.
That's the idea going in anyway, so it would basically replace all the hand-coded methods in the oxml sub-package with declarations of the right types.
@vrvolle Can you contact me via email at stcanny at geemail dot com? There are a few things about the commits I wanted to discuss with you if you have a minute.
Activity
scanny commentedon Feb 28, 2014
The documentation is available here.
The
Paragaph.styleattribute allows a paragraph style to be applied. The font typeface, size, and color can all be specified in a paragraph style.There is not yet a way to apply a character style to a run. That will be an important and useful feature.
There is also not yet a direct way to manipulate a style in python-docx. That may also turn out to be an important feature.
A way to apply character formatting directly to a run will probably be last of these to come, since the effect can be achieved by defining a character style and applying it.
vakano commentedon Mar 4, 2014
thanks for the feedback, so does it mean we can not set a paragraph to this specific example for current ? font(eg:Calibri) size(eg:10.5) color(eg red).
BTW: I am not clear about the meanning of "a run", can you give some explanations?
scanny commentedon Mar 4, 2014
A run is a sequence of characters within a paragraph that share the same character formatting. So a single bold word in a paragraph would be in a separate run. That paragraph would need three runs, containing the text before the bold word, the text of the bold word, and the text after the bold word respectively.
You should be able to achieve the effect you're describing by creating a style in the "template" document you're using that has the font characteristics you want and then applying that paragraph style to the paragraph.
scanny commentedon Apr 5, 2014
@vrvolle In the XML, applying a style to a run (a character style) looks like this:
If you're willing to go with a workaround until this can be implemented, something like this should do the trick:
I haven't tested this, so let me know if it gives you trouble and I can help you troubleshoot.
You'll need to discover the actual character style names by looking them up in the styles.xml of the document or by guessing. They should match the GUI names for the styles, but with spaces collapsed and capitalization possibly different.
An easy way to browse the styles.xml is by using opc-diag.
Let me know if you need more.
kontrafiktion commentedon Apr 7, 2014
works like a charm
thx
kontrafiktion commentedon Apr 7, 2014
I cloned the repo and tried to run
but it runs 0 tests:
What do I have to do, if I want to start hacking the module (I am very new to Python, but wanted to give it a try)
kontrafiktion commentedon Apr 7, 2014
this seems to work: invoking
in the 'tests' directory.
B.t.w.: running
py.test-3.4results in a tremendous bunch of errors. Mostlyscanny commentedon Apr 7, 2014
should run py.test in the main python-docx dir
Also behave
so:
$ cd python-docx $ py.test ... 551 tests $ behave ... 126 scenarios passed ...kontrafiktion commentedon Apr 7, 2014
Thx.
I have started to create a patch at https://github.com/vrvolle/python-docx/tree/character-style
It is already working. I just copied the implementation for pStyle (though I didn't like the code duplication and having to work through "Run", "CT_R" and "CT_RPr" -- but as I said, I am very new to Python). What I really don't understand: why in one place there is
to access the value and
at another.
Still missing: tests and documentation (try to finish until end of week).
scanny commentedon Apr 7, 2014
Yep, that former one could be changed to look like the latter. Probably just an artifact of coding sequence. I've been a little loose with the .oxml subpackages because most of that code will be replaced by declarations that drive a configurable metaclass, once I get caught up a little further and can go heads down on that architectural bit :)
Looks great so far, never mind the seeming duplication, most or all of that part will get swallowed up by the metaprogramming once I'm done :)
kontrafiktion commentedon Apr 24, 2014
"Looks great so far, never mind the seeming duplication, most or all of that part will get swallowed up by the metaprogramming once I'm done :)"
I need some more features (cross references, footnotes -- everything needed for multimarkdown). Should I wait for you to be done or start hacking?
How do you want to change the impl. with metaprogramming, via
__getattr__magic or by using decorators extensively? Just curious.scanny commentedon Apr 24, 2014
Hi Victor, were you going to add an acceptance test for that feature? I think that's the only part outstanding on the character styles. This acceptance test is pretty close as an example: https://github.com/python-openxml/python-docx/blob/master/features/par-set-style.feature
and uses these steps: https://github.com/python-openxml/python-docx/blob/master/features/steps/paragraph.py.
Happy to have you work on other features. Best to start with determining the API. I use a page like this one in the developer documentation to get that sorted out: http://python-docx.readthedocs.org/en/latest/dev/analysis/features/breaks.html. Once you have a proposal there we can discuss and resolve. Then I start with a failing acceptance test. Both of these are separately committable steps even if there's no implementation.
Some folks like to spike then, to get the implementation working. But ultimately the work product that's the least work for me to commit (read: fastest time from pull request to commit) is working outside in, one test at a time. Here's a fairly typical example, the four commits starting with this one for adding the
.boldproperty to a run: d04bbd0.On the metaprogramming bit, what I had planned to do was add methods to a given class for the particular child elements and attributes it had, based on a declarative definition in the class. Roughly like what SQLAlchemy or Django does. So it would be something like:
which would produce a class that had the following properties and methods:
The properties and methods would include ones for each child element and attribute, so it would get to be something of a long list. Implementations would generally be with standard closures define in or around the MetaClass.
That's the idea going in anyway, so it would basically replace all the hand-coded methods in the oxml sub-package with declarations of the right types.
kontrafiktion commentedon Apr 27, 2014
done
scanny commentedon May 1, 2014
SchwEEEt! Incorporating now ...
release: v0.5.2 #17 feature: character style
scanny commentedon May 7, 2014
Committed to master and develop and released to PyPI as v0.5.2.
scanny commentedon May 7, 2014
@vrvolle Can you contact me via email at stcanny at geemail dot com? There are a few things about the commits I wanted to discuss with you if you have a minute.
Maginx commentedon Dec 30, 2015
Hi,
I used this API to write word document, but I find I set the font name is not effectual to Chinese characters. Do you have any resolution?
code snippet :
title = docx.add_paragraph(unicode(article.title.content,'gbk'))
title_run = title.runs[0]
parag_format = title.paragraph_format
title.line_spacing_rule = WD_LINE_SPACING.MULTIPLE
parag_format.alignment = TITLE_STYLE
parag_format.line_spacing = TITLE_PARA_STYLE
title_run.font.name = TITLE_FONT_NAME
title_run.font.size = Pt(TITLE_FONT_SIZE)
feat: support `fldChar` and `instrText` elements (python-openxml#17)