diff --git a/docx.py b/docx.py index 9d3f6e6..617f9f0 100755 --- a/docx.py +++ b/docx.py @@ -293,9 +293,33 @@ def heading(headingtext, headinglevel, lang='en'): # Return the combined paragraph return paragraph +def cell_style(row, col): + """ + Default function for table cell styles. + + @param int row: Cell's row number + @param int col: Cell's column number + + @return dict: dict with 2 supported keys: + 'style': dict of styles + 'align': specify the alignment, see paragraph + documentation. + """ + default_style = {'style': {'val': 'clear', + 'color': 'auto', + 'fill': 'FFFFFF', + 'themeFill': 'text2', + 'themeFillTint': '99'}, + 'align': 'left'} + + # default center alignment for header + if not row: + default_style['align'] = 'center' + + return default_style -def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, - twunit='auto', borders={}, celstyle=None): +def table(contents, colw=None, cwunit='dxa', tblw=0, + twunit='auto', borders=None, style_func=cell_style): """ Return a table element based on specified parameters @@ -303,8 +327,6 @@ def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, the list can be a string or a valid XML element itself. It can also be a list. In that case all the listed elements will be merged into the cell. - @param bool heading: Tells whether first line should be treated as - heading or not @param list colw: list of integer column widths specified in wunitS. @param str cwunit: Unit used for column width: 'pct' : fiftieths of a percent @@ -327,10 +349,7 @@ def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, a point val : The style of the border, see http://www.schemacentral.com/sc/ooxml/t-w_ST_Border.htm - @param list celstyle: Specify the style for each colum, list of dicts. - supported keys: - 'align' : specify the alignment, see paragraph - documentation. + @return lxml.etree: Generated XML etree element """ table = makeelement('tbl') @@ -342,13 +361,13 @@ def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, tablewidth = makeelement( 'tblW', attributes={'w': str(tblw), 'type': str(twunit)}) tableprops.append(tablewidth) - if len(borders.keys()): + if borders: tableborders = makeelement('tblBorders') for b in ['top', 'left', 'bottom', 'right', 'insideH', 'insideV']: - if b in borders.keys() or 'all' in borders.keys(): - k = 'all' if 'all' in borders.keys() else b + if b in borders or 'all' in borders: + k = 'all' if 'all' in borders else b attrs = {} - for a in borders[k].keys(): + for a in borders[k]: attrs[a] = unicode(borders[k][a]) borderelem = makeelement(b, attributes=attrs) tableborders.append(borderelem) @@ -368,50 +387,22 @@ def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, cnfStyle = makeelement('cnfStyle', attributes={'val': '000000100000'}) rowprops.append(cnfStyle) row.append(rowprops) - if heading: - i = 0 - for heading in contents[0]: - cell = makeelement('tc') - # Cell properties - cellprops = makeelement('tcPr') - if colw: - wattr = {'w': str(colw[i]), 'type': cwunit} - else: - wattr = {'w': '0', 'type': 'auto'} - cellwidth = makeelement('tcW', attributes=wattr) - cellstyle = makeelement('shd', attributes={'val': 'clear', - 'color': 'auto', - 'fill': 'FFFFFF', - 'themeFill': 'text2', - 'themeFillTint': '99'}) - cellprops.append(cellwidth) - cellprops.append(cellstyle) - cell.append(cellprops) - # Paragraph (Content) - if not isinstance(heading, (list, tuple)): - heading = [heading] - for h in heading: - if isinstance(h, etree._Element): - cell.append(h) - else: - cell.append(paragraph(h, jc='center')) - row.append(cell) - i += 1 - table.append(row) # Contents Rows - for contentrow in contents[1 if heading else 0:]: + for i, contentrow in enumerate(contents): row = makeelement('tr') - i = 0 - for content in contentrow: + + for k, content in enumerate(contentrow): cell = makeelement('tc') # Properties cellprops = makeelement('tcPr') if colw: - wattr = {'w': str(colw[i]), 'type': cwunit} + wattr = {'w': str(colw[k]), 'type': cwunit} else: wattr = {'w': '0', 'type': 'auto'} cellwidth = makeelement('tcW', attributes=wattr) + cellstyle = makeelement('shd', attributes=style_func(i, k)['style']) cellprops.append(cellwidth) + cellprops.append(cellstyle) cell.append(cellprops) # Paragraph (Content) if not isinstance(content, (list, tuple)): @@ -420,13 +411,9 @@ def table(contents, heading=True, colw=None, cwunit='dxa', tblw=0, if isinstance(c, etree._Element): cell.append(c) else: - if celstyle and 'align' in celstyle[i].keys(): - align = celstyle[i]['align'] - else: - align = 'left' - cell.append(paragraph(c, jc=align)) + cell.append(paragraph(c, jc=style_func(i, k)['align'])) row.append(cell) - i += 1 + table.append(row) return table diff --git a/setup.py b/setup.py index 13035e6..3ba8eba 100644 --- a/setup.py +++ b/setup.py @@ -18,7 +18,7 @@ setup( name='docx', - version='0.2.4', + version='0.3.0', install_requires=['lxml', 'Pillow>=2.0'], description=DESCRIPTION, author='Mike MacCana', diff --git a/template/[Content_Types].xml b/template/[Content_Types].xml new file mode 100644 index 0000000..f131d97 --- /dev/null +++ b/template/[Content_Types].xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/template/_rels/.rels b/template/_rels/.rels index 19e8aca..61a69d5 100644 --- a/template/_rels/.rels +++ b/template/_rels/.rels @@ -1,12 +1,12 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/template/word/_rels/document.xml.rels b/template/word/_rels/document.xml.rels new file mode 100644 index 0000000..3b2b7f8 --- /dev/null +++ b/template/word/_rels/document.xml.rels @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/template/word/fontTable.xml b/template/word/fontTable.xml index e66fe2c..12aa273 100644 --- a/template/word/fontTable.xml +++ b/template/word/fontTable.xml @@ -1,52 +1,2 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + \ No newline at end of file diff --git a/template/word/settings.xml b/template/word/settings.xml index 92dfa3f..0c94e90 100644 --- a/template/word/settings.xml +++ b/template/word/settings.xml @@ -1,43 +1,2 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/template/word/styles.xml b/template/word/styles.xml index 7471355..002b868 100644 --- a/template/word/styles.xml +++ b/template/word/styles.xml @@ -1,2 +1,2 @@ - - \ No newline at end of file + + \ No newline at end of file diff --git a/template/word/theme/theme1.xml b/template/word/theme/theme1.xml index fae0229..1de629e 100644 --- a/template/word/theme/theme1.xml +++ b/template/word/theme/theme1.xml @@ -1,2 +1,2 @@ - - \ No newline at end of file + + \ No newline at end of file