Open
Description
I use this code to get the shading color on run and on paragraphs, how to add a function on run and paragraph in python-docx code repo to get shading color when it is found ? I found this feature missing in the python-docx.
from lxml import etree
def get_paragraph_shading_color(xml_paragraph_str):
paragraph_xml = etree.fromstring(xml_paragraph_str)
attrib_fill = None
namespaces = {'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
try:
namespaces = {paragraph_xml.prefix : paragraph_xml.nsmap[paragraph_xml.prefix]}
except:
#print("Could not determine namespace")
pass
attrib_fill = None
for e in paragraph_xml.findall('.//w:pPr/w:shd', namespaces):
#print("e:", etree.tostring(e, pretty_print=True))
try:
attrib_val = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val')
attrib_color = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}color')
attrib_fill = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}fill')
#print(f"attrib_color : {attrib_color}")
#print(f"attrib_fill : {attrib_fill}")
#print(f"attrib_val : {attrib_val}")
except:
pass
return attrib_fill
def get_run_shading_color(xml_run_str):
run_xml = etree.fromstring(xml_run_str)
namespaces = {'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
try:
namespaces = {run_xml.prefix : run_xml.nsmap[run_xml.prefix]}
except:
#print("Could not determine namespace")
pass
attrib_fill = None
for e in run_xml.findall('.//w:rPr/w:shd', namespaces):
#print("e:", etree.tostring(e, pretty_print=True))
try:
attrib_val = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val')
attrib_color = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}color')
attrib_fill = e.attrib.get('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}fill')
except:
pass
return attrib_fill
root = etree.fromstring(run.element.xml)
run_shading_color = get_run_shading_color(run.element.xml)
root = etree.fromstring(paragraph._p.xml)
p_shading_color = get_paragraph_shading_color(paragraph._p.xml)