Skip to content

feature: document lxml element of proxy classes for advanced users #7

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
scanny opened this issue Jan 15, 2014 · 5 comments
Open

Comments

@scanny
Copy link
Contributor

scanny commented Jan 15, 2014

Proxy classes such as Document, Paragraph, and Table each hold a private reference to the lxml element they correspond to, <w:document>, <w:p>, and <w:tbl> respectively. With these elements, advanced users can call the underlying lxml API directly to develop customized solutions the existing API does not yet support.

Add documentation so advanced users can readily access these elements without consulting the source code.

@scanny scanny mentioned this issue Jan 15, 2014
Closed
@notionparallax
Copy link

Hi, is there anything I can do to help this along?
I need to get to the formatting on a run, and this seems like the only way to do it. (I'm processing existing documents)

<w:r>
  <w:rPr>
    <w:rFonts w:ascii="HelveticaNeueLT Com 45 Lt" w:cs="Times New Roman" w:eastAsia="Calibri" w:hAnsi="HelveticaNeueLT Com 45 Lt"/>
    <w:sz w:val="20"/>
    <w:szCs w:val="20"/>
    <w:shd w:fill="00FF00" w:val="clear"/>  <=== THE BIT I WANT
  </w:rPr>
  <w:t>Public Building/Cultural</w:t>
</w:r>

Handling just this bit to pull out the shading is fine, but parsing the whole doc seems way too much like hard work!

Update

I got around this in a bit of a hacky way with this:

def isShaded(aRun):
    return "<w:shd" in aRun._r.xml

@scanny
Copy link
Contributor Author

scanny commented Mar 19, 2014

You can access this element with the lxml API using something like this:

>>> from docx.oxml.shared import qn
>>> assert isinstance(Run, run)
>>> rPr = run._r.rPr
>>> shd = rPr.find(qn('w:shd'))  # returns the shd child element or None if not present
>>> shd.get(qn('w:fill'))
'00FF00'
>>> shd.get(qn('w:val'))
'clear'

@danmilon
Copy link

I'm making use of the underlying lxml API too because I need to store the XPath of some runs. I'd feel more confident if it was documented and I was assured the property name wont change in the future.

@onlyjus
Copy link
Contributor

onlyjus commented Apr 27, 2014

I added the shading element in my merge request. Have it functioning for table cells. Should be easily added to runs.

@scanny scanny added this to the v0.7.0 milestone May 1, 2014
@scanny scanny modified the milestones: v0.8.0, v0.7.0 Table Enhancements, later May 13, 2014
@scanny scanny modified the milestones: later, Release v0.8.6 Apr 9, 2016
@scanny
Copy link
Contributor Author

scanny commented Aug 12, 2021

@progamniac please ask support questions on StackOverflow using the python-docx tag.

@python-openxml python-openxml deleted a comment from progamniac Aug 12, 2021
ryanamannion referenced this issue in BlackBoiler/python-docx Sep 8, 2021
alberto743 pushed a commit to alberto743/python-docx that referenced this issue Jun 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants