コード例 #1
0
ファイル: table.py プロジェクト: lucascastejon/py2docx
    def _set_padding(self):
        if type(self.padding) in [str, unicode]:
            sizes = self.padding.split(' ')
            if len(sizes) == 1:
                top = bottom = left = right = sizes[0]
            elif len(sizes) == 2:
                top = bottom = sizes[0]
                left = right = sizes[1]
            elif len(sizes) == 3:
                top = sizes[0]
                right = left = sizes[1]
                bottom = sizes[2]
            elif len(sizes) == 4:
                top = sizes[0]
                right = sizes[1]
                bottom = sizes[2]
                left = sizes[3]
            else:
                raise InvalidCellPaddingException("Table Padding should be " +
                                                  "in the W3C CSS format")

            top = Unit.to_dxa(top)
            bottom = Unit.to_dxa(bottom)
            right = Unit.to_dxa(right)
            left = Unit.to_dxa(left)

            xml = '\n<w:tblCellMar>' + \
                  '<w:top w:w="{top}" w:type="dxa" />' + \
                  '<w:left w:w="{left}" w:type="dxa" />' + \
                  '<w:bottom w:w="{bottom}" w:type="dxa" />' + \
                  '<w:right w:w="{right}" w:type="dxa" />' + \
                  '</w:tblCellMar>\n'

            self.xml_props.append(xml.format(top=top,
                                             bottom=bottom,
                                             right=right,
                                             left=left))
コード例 #2
0
ファイル: table.py プロジェクト: lucascastejon/py2docx
    def _set_width(self):
        if self.width:
            xml = '\n<w:tcW w:type="{unit_type}" w:w="{width}"/>\n'
            if self.width[-1:] == '%':
                unit_type = 'pct'
                width = int(self.width[:-1])
                if width <= 100 and width > 0:
                    width = width * 50
                    xml = xml.format(unit_type=unit_type,
                                     width=width)
                else:
                    return

            elif self.width[-2:] in ['cm', 'pt', 'in']:
                unit_type = 'dxa'
                width = Unit.to_dxa(self.width)
                xml = xml.format(unit_type=unit_type,
                                 width=width)

            self.xml_props.append(xml)