Created
September 3, 2019 08:22
-
-
Save chaseliu/d00067b2f7466aefaca7029efaac02da to your computer and use it in GitHub Desktop.
演示如何使用python-docx创建团体报告
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
演示如何使用python-docx创建团体报告 | |
""" | |
import functools | |
import itertools | |
import operator | |
from docx import Document | |
from docx.table import Table | |
def format_paragraphs(self): | |
for row in self.rows: | |
for cell in row.cells: | |
for paragraph in cell.paragraphs: | |
paragraph.style = 'No Spacing' | |
Table.format_paragraphs = format_paragraphs | |
class TableBuilder(object): | |
"""获取Document对象与表格数据,创建Table对象""" | |
def __init__(self, document, columns, index, data, style='TableStyle'): | |
self.document = document | |
self.columns = columns | |
self.index = index | |
self.data = data | |
self.style = style | |
self.table = None | |
def _fill_columns(self): | |
"""根据初始化的columns,使用填写表头单元格""" | |
repeats = 1 | |
steps = functools.reduce(operator.mul, map(len, self.columns)) | |
for i, column in enumerate(self.columns): | |
steps //= len(column) | |
for j, col in enumerate(column * repeats): | |
cell_l = self.table.cell(i, steps * j + 1) | |
cell_r = self.table.cell(i, steps * (j + 1)) | |
merged_cell = cell_l.merge(cell_r) | |
merged_cell.text = col | |
repeats *= len(column) | |
def _fill_index(self): | |
columns_len = len(self.columns) | |
cell_t = self.table.cell(0, 0) | |
cell_b = self.table.cell(columns_len - 1, 0) | |
cell_t.merge(cell_b) | |
for i, text in enumerate(self.index): | |
self.table.cell(i + columns_len, 0).text = text | |
def _fill_data(self): | |
columns_len = len(self.columns) | |
for i, row in enumerate(self.data): | |
for j, text in enumerate(row): | |
self.table.cell(columns_len + i, 1 + j).text = text | |
def build(self): | |
self.table = document.add_table( | |
rows=len(self.index) + len(self.columns), | |
cols=functools.reduce(operator.mul, map(len, self.columns)) + 1, | |
style=self.style | |
) | |
self._fill_columns() | |
self._fill_index() | |
self._fill_data() | |
return self.table | |
def section_one(document): | |
document.add_heading('概要', level=1) | |
document.add_paragraph('2018年国网江苏公司信息通信分公司共92人进行了健康体检,久康云通过对上述人员体检结果的汇总分析,形成本报告。') | |
document.add_paragraph('本报告是针对贵单位员工体检结果和健康调查的综合分析报告,通过本报告可以让贵单位管理者了解员工的整体身体健康状况(包括面临的疾病、风险和发展趋势),进而采取有针对性的干预和管理措施,降低员工的疾病风险,实现员工健康资产的保障、升值,提升企业的劳动生产率和可持续发展能力。') | |
document.add_page_break() | |
def section_two(document): | |
document.add_heading('基本资料', level=1) | |
document.add_heading('年龄及性别分布', level=2) | |
columns = [['35岁以下', '36岁-45岁', '46岁-55岁', '56岁以上', '合计'], ['人数', '占比']] | |
index = ['男性', '女性', '合计'] | |
data = [['1'] * 10] * 3 | |
table = TableBuilder(document, columns=columns, index=index, data=data).build() | |
table.format_paragraphs() | |
if __name__ == '__main__': | |
document = Document('排版样例.docx') | |
section_one(document) | |
section_two(document) | |
document.save('demo.docx') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment