def style(self): styles = { 'body': { 'margin': '0px auto', } } if self.page_width: width = self.page_width / POINTS_PER_EM styles['body']['width'] = '%.2fem' % width result = [] for name, definition in sorted(PYDOCX_STYLES.items()): result.append('.pydocx-%s {%s}' % ( name, convert_dictionary_to_style_fragment(definition), )) for name, definition in sorted(styles.items()): result.append('%s {%s}' % ( name, convert_dictionary_to_style_fragment(definition), )) tag = HtmlTag('style') return tag.apply(''.join(result))
def style(self): styles = { 'body': { 'margin': '0px auto', } } if self.page_width: width = self.page_width / POINTS_PER_EM styles['body']['width'] = '%.2fem' % width result = [] for name, definition in sorted(PYDOCX_STYLES.items()): result.append('.pydocx-%s {%s}' % ( name, convert_dictionary_to_style_fragment(definition), )) for name, definition in sorted(styles.items()): result.append('%s {%s}' % ( name, convert_dictionary_to_style_fragment(definition), )) return self.make_element( tag='style', contents=''.join(result), )
def indent( self, text, alignment=None, firstLine=None, left=None, right=None, ): attrs = {} if alignment: attrs['class'] = 'pydocx-%s' % alignment style = {} if firstLine: firstLine = self._convert_measurement(firstLine) style['text-indent'] = '%.2fem' % firstLine if left: left = self._convert_measurement(left) style['margin-left'] = '%.2fem' % left if right: right = self._convert_measurement(right) style['margin-right'] = '%.2fem' % right if style: attrs['style'] = convert_dictionary_to_style_fragment(style) return self.make_element( tag='span', contents=text, attrs=attrs, )
def export_numbering_item(self, numbering_item): results = self.yield_nested_with_line_breaks_between_paragraphs( numbering_item.children, self.export_node, ) style = None if numbering_item.children: level_properties = numbering_item.numbering_span.\ numbering_level.paragraph_properties # get the first paragraph properties which will contain information # on how to properly indent listing item paragraph = numbering_item.children[0] style = self.export_listing_paragraph_property_indentation( paragraph, level_properties) attrs = {} if style: attrs['style'] = convert_dictionary_to_style_fragment(style) tag = HtmlTag('li', **attrs) return tag.apply(results)
def export_numbering_item(self, numbering_item): results = self.yield_nested_with_line_breaks_between_paragraphs( numbering_item.children, self.export_node, ) style = None if numbering_item.children: level_properties = numbering_item.numbering_span.\ numbering_level.paragraph_properties # get the first paragraph properties which will contain information # on how to properly indent listing item paragraph = numbering_item.children[0] style = self.export_listing_paragraph_property_indentation(paragraph, level_properties) attrs = {} if style: attrs['style'] = convert_dictionary_to_style_fragment(style) tag = HtmlTag('li', **attrs) return tag.apply(results)
def export_paragraph_property_indentation(self, paragraph, results): # TODO these classes should be applied on the paragraph, and not as # inline styles properties = paragraph.effective_properties style = {} if properties.indentation_right: # TODO would be nice if this integer conversion was handled # implicitly by the model somehow try: right = int(properties.indentation_right) except ValueError: right = None if right: right = convert_twips_to_ems(right) style['margin-right'] = '{0:.2f}em'.format(right) if properties.indentation_left: # TODO would be nice if this integer conversion was handled # implicitly by the model somehow try: left = int(properties.indentation_left) except ValueError: left = None if left: left = convert_twips_to_ems(left) style['margin-left'] = '{0:.2f}em'.format(left) if properties.indentation_first_line: # TODO would be nice if this integer conversion was handled # implicitly by the model somehow try: first_line = int(properties.indentation_first_line) except ValueError: first_line = None if first_line: first_line = convert_twips_to_ems(first_line) # TODO text-indent doesn't work with inline elements like span style['text-indent'] = '{0:.2f}em'.format(first_line) if style: attrs = { 'style': convert_dictionary_to_style_fragment(style) } tag = HtmlTag('span', **attrs) results = tag.apply(results, allow_empty=False) return results
def export_paragraph_property_indentation(self, paragraph, results): # TODO these classes should be applied on the paragraph, and not as # inline styles properties = paragraph.effective_properties style = {} # Numbering properties can define a text indentation on a paragraph if properties.numbering_properties: indentation_left = None indentation_first_line = None paragraph_num_level = paragraph.get_numbering_level() if paragraph_num_level: listing_style = self.export_listing_paragraph_property_indentation( paragraph, paragraph_num_level.paragraph_properties, include_text_indent=True ) if 'text-indent' in listing_style and listing_style['text-indent'] != '0.00em': style['text-indent'] = listing_style['text-indent'] style['display'] = 'inline-block' else: indentation_left = properties.to_int('indentation_left') indentation_first_line = properties.to_int('indentation_first_line') indentation_right = properties.to_int('indentation_right') if indentation_right: right = convert_twips_to_ems(indentation_right) style['margin-right'] = '{0:.2f}em'.format(right) if indentation_left: left = convert_twips_to_ems(indentation_left) style['margin-left'] = '{0:.2f}em'.format(left) if indentation_first_line: first_line = convert_twips_to_ems(indentation_first_line) style['text-indent'] = '{0:.2f}em'.format(first_line) style['display'] = 'inline-block' if style: attrs = { 'style': convert_dictionary_to_style_fragment(style) } tag = HtmlTag('span', **attrs) results = tag.apply(results, allow_empty=False) return results
def export_paragraph_property_indentation(self, paragraph, results): # TODO these classes should be applied on the paragraph, and not as # inline styles properties = paragraph.effective_properties style = {} if properties.indentation_right: # TODO would be nice if this integer conversion was handled # implicitly by the model somehow try: right = int(properties.indentation_right) except ValueError: right = None if right: right = convert_twips_to_ems(right) style['margin-right'] = '{0:.2f}em'.format(right) if properties.indentation_left: # TODO would be nice if this integer conversion was handled # implicitly by the model somehow try: left = int(properties.indentation_left) except ValueError: left = None if left: left = convert_twips_to_ems(left) style['margin-left'] = '{0:.2f}em'.format(left) if properties.indentation_first_line: # TODO would be nice if this integer conversion was handled # implicitly by the model somehow try: first_line = int(properties.indentation_first_line) except ValueError: first_line = None if first_line: first_line = convert_twips_to_ems(first_line) # TODO text-indent doesn't work with inline elements like span style['text-indent'] = '{0:.2f}em'.format(first_line) if style: attrs = {'style': convert_dictionary_to_style_fragment(style)} tag = HtmlTag('span', **attrs) results = tag.apply(results, allow_empty=False) return results
def style(self): styles = { 'p': { 'white-space': 'normal', 'line-height': '24px', }, 'span': { 'font-size': '14px', 'font-family': '宋体' }, '.pydocx-center': { 'text-align': ' center' } } result = [] for name, definition in sorted(styles.items()): result.append('%s {%s}' % ( name, convert_dictionary_to_style_fragment(definition), )) tag = HtmlTag('style') return tag.apply(''.join(result))