示例#1
0
        color = self._color
        if color is None:
            return color
        return color.val

    @color.setter
    def color(self, color):
        if color is None:
            self._remove__color()
        elif self._color is None:
            self._add__color(val=color)
        else:
            self._color.val = color


register_element_cls('w:rPr', CT_RPr2)
register_element_cls('w:color', CT_String)


def _r_color_getter(self):
    """
    String contained in w:color element of <w:rStyle> grandchild, or
    |None| if that element is not present.
    """
    # noinspection PyPep8Naming
    rPr = self.rPr
    if rPr is None:
        return None
    return rPr.color

nsmap['mc'] = 'http://schemas.openxmlformats.org/markup-compatibility/2006'
nsmap[
    'wps'] = 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
nsmap['v'] = 'urn:schemas-microsoft-com:vml'


class CT_TxbxContent(BaseOxmlElement):
    """
    ``<w:rStyle>`` element, containing the properties for a style.
    """
    p = ZeroOrMore('w:p')
    tbl = ZeroOrMore('w:tbl')


register_element_cls('w:txbxContent', CT_TxbxContent)


class Textbox(Parented):
    def __init__(self, parent, element):
        super(Textbox, self).__init__(parent)
        self._tb = element

    @property
    def paragraphs(self):
        return [Paragraph(parent=self, p=p) for p in self._tb.p_lst]

    @property
    def tables(self):
        return [Table(parent=self, tbl=t) for t in self._tb.tbl_lst]
示例#3
0
            '  <wp:positionV relativeFrom="page">\n'
            '    <wp:posOffset>%d</wp:posOffset>\n'
            '  </wp:positionV>\n'                    
            '  <wp:extent cx="914400" cy="914400"/>\n'
            '  <wp:wrapNone/>\n'
            '  <wp:docPr id="666" name="unnamed"/>\n'
            '  <wp:cNvGraphicFramePr>\n'
            '    <a:graphicFrameLocks noChangeAspect="1"/>\n'
            '  </wp:cNvGraphicFramePr>\n'
            '  <a:graphic>\n'
            '    <a:graphicData uri="URI not set"/>\n'
            '  </a:graphic>\n'
            '</wp:anchor>' % ( nsdecls('wp', 'a', 'pic', 'r'), int(pos_x), int(pos_y) )
        )

register_element_cls('wp:anchor', _CT_Anchor)


def add_float_image(p, image_path_or_stream, width, pos_x=None, pos_y=None):
    '''Add float image behind text.
    
    Args:
        p (Paragraph): ``python-docx`` Paragraph object this picture belongs to.
        image_path_or_stream (str, bytes): Image path or stream.
        width (float): Displaying width of picture, in unit Pt.
        pos_x (float): X-position (English Metric Units) to the top-left point of page valid region
        pos_y (float): Y-position (English Metric Units) to the top-left point of page valid region
    '''
    run = p.add_run()
    # parameters for picture, e.g. id, name
    rId, image = run.part.get_or_add_image(image_path_or_stream)
示例#4
0
from docx.oxml import register_element_cls, nsmap

from question_parser.equation.mathtype.docx_ext import CT_OBJECT, CT_OLEOBJECT, CT_R2, CT_OMath, CT_OMathPara

nsmap['o'] = ('urn:schemas-microsoft-com:office:office')
nsmap['m'] = ('http://schemas.openxmlformats.org/officeDocument/2006/math')
nsmap['v']=('urn:schemas-microsoft-com:vml')
register_element_cls('w:object', CT_OBJECT)
register_element_cls('o:OLEObject', CT_OLEOBJECT)
register_element_cls('w:r', CT_R2)
register_element_cls('m:oMathPara', CT_OMathPara)
register_element_cls('m:oMath', CT_OMath)
示例#5
0
 def it_uses_registered_element_classes(self, xml_bytes):
     register_element_cls('a:foo', CustElmCls)
     element = parse_xml(xml_bytes)
     assert isinstance(element, CustElmCls)
__author__ = 'bluec0re'

nsmap['mc'] = 'http://schemas.openxmlformats.org/markup-compatibility/2006'
nsmap['wps'] = 'http://schemas.microsoft.com/office/word/2010/wordprocessingShape'
nsmap['v'] = 'urn:schemas-microsoft-com:vml'


class CT_TxbxContent(BaseOxmlElement):
    """
    ``<w:rStyle>`` element, containing the properties for a style.
    """
    p = ZeroOrMore('w:p')
    tbl = ZeroOrMore('w:tbl')


register_element_cls('w:txbxContent', CT_TxbxContent)


class Textbox(Parented):
    def __init__(self, parent, element):
        super(Textbox, self).__init__(parent)
        self._tb = element

    @property
    def paragraphs(self):
        return [Paragraph(parent=self, p=p) for p in self._tb.p_lst]

    @property
    def tables(self):
        return [Table(parent=self, tbl=t) for t in self._tb.tbl_lst]
 def it_uses_registered_element_classes(self, xml_bytes):
     register_element_cls('a:foo', CustElmCls)
     element = parse_xml(xml_bytes)
     assert isinstance(element, CustElmCls)
示例#8
0
 def it_determines_class_used_for_elements_with_matching_tagname(
         self, xml_text):
     register_element_cls('a:foo', CustElmCls)
     foo = parse_xml(xml_text)
     assert type(foo) is CustElmCls
     assert type(foo.find(qn('a:bar'))) is etree._Element
 def it_determines_class_used_for_elements_with_matching_tagname(
         self, xml_text):
     register_element_cls('a:foo', CustElmCls)
     foo = parse_xml(xml_text)
     assert type(foo) is CustElmCls
     assert type(foo.find(qn('a:bar'))) is etree._Element
from docx.enum.text import WD_BREAK, WD_TAB_ALIGNMENT, WD_TAB_LEADER
from docx.oxml import register_element_cls
from docx.oxml.simpletypes import XsdInt
# monkey patch
from docx.oxml.xmlchemy import BaseOxmlElement, ZeroOrOne

package_dir = os.path.abspath(os.path.dirname(__file__))

logger = logging.getLogger(__name__)


class CT_TrPr(BaseOxmlElement):
    tblHeader = ZeroOrOne('w:tblHeader')


register_element_cls('w:trPr', CT_TrPr)


class DocxWriter(writers.Writer):
    supported = ('docx', )
    settings_spec = ('No options here.', '', ())
    settings_defaults = {}  # type: Dict

    output = None

    def __init__(self, builder):
        # type: (DocxBuilder) -> None
        writers.Writer.__init__(self)
        self.builder = builder

        stylefile = builder.config.docx_style
示例#11
0
class CT_ZomChild(BaseOxmlElement):
    """
    Zom standing for 'ZeroOrMore', ``<w:zomChild>`` element, representing an
    optional child element that can appear multiple times in sequence.
    """


class CT_ZooChild(BaseOxmlElement):
    """
    Zoo standing for 'ZeroOrOne', ``<w:zooChild>`` element, an invented
    element for use in testing.
    """


register_element_cls('w:parent', CT_Parent)
register_element_cls('w:choice', CT_Choice)
register_element_cls('w:oomChild', CT_OomChild)
register_element_cls('w:zomChild', CT_ZomChild)
register_element_cls('w:zooChild', CT_ZooChild)


class CT_ChoiceBuilder(BaseBuilder):
    __tag__ = 'w:choice'
    __nspfxs__ = ('w', )
    __attrs__ = ()


class CT_Choice2Builder(BaseBuilder):
    __tag__ = 'w:choice2'
    __nspfxs__ = ('w', )
示例#12
0
        color = self._color
        if color is None:
            return color
        return color.val

    @color.setter
    def color(self, color):
        if color is None:
            self._remove__color()
        elif self._color is None:
            self._add__color(val=color)
        else:
            self._color.val = color


register_element_cls('w:rPr', CT_RPr2)
register_element_cls('w:color', CT_String)


def _r_color_getter(self):
    """
    String contained in w:color element of <w:rStyle> grandchild, or
    |None| if that element is not present.
    """
    # noinspection PyPep8Naming
    rPr = self.rPr
    if rPr is None:
        return None
    return rPr.color

示例#13
0
class CT_ZomChild(BaseOxmlElement):
    """
    Zom standing for 'ZeroOrMore', ``<w:zomChild>`` element, representing an
    optional child element that can appear multiple times in sequence.
    """


class CT_ZooChild(BaseOxmlElement):
    """
    Zoo standing for 'ZeroOrOne', ``<w:zooChild>`` element, an invented
    element for use in testing.
    """


register_element_cls('w:parent',   CT_Parent)
register_element_cls('w:choice',   CT_Choice)
register_element_cls('w:oomChild', CT_OomChild)
register_element_cls('w:zomChild', CT_ZomChild)
register_element_cls('w:zooChild', CT_ZooChild)


class CT_ChoiceBuilder(BaseBuilder):
    __tag__ = 'w:choice'
    __nspfxs__ = ('w',)
    __attrs__ = ()


class CT_Choice2Builder(BaseBuilder):
    __tag__ = 'w:choice2'
    __nspfxs__ = ('w',)