Skip to content

基于表格的周报及项目总结生成器

python

def read_file(filename):
    with open(filename, 'r', encoding='utf-8') as fp:
        return fp.read()

def write_file(filename, content):
    with open(filename, 'w', encoding='utf-8') as fp:
        fp.write(content)

class MarkdownParser:
    def __init__(self, content: str):
        self.content = content

    def parse_md_table(self, md_table_text: str) -> list:
        lines = md_table_text.split('\n')
        ks = lines[0].strip(' | ').split(' | ')
        l = []
        for line in lines[2:-1]:
            d = dict()
            line_items = line.strip(' | ').split(' | ')
            for i, e in enumerate(line_items):
                d[ks[i]] = e
            l.append(d)
        return l

    def get_structure_data(self) -> list:
        day_items = self.content.split("## ")[1:]

        l = list()
        for day_item in day_items:
            day, item = day_item.split('\n', 1)
            d = dict()
            d['date_range'] = day.split('-')
            d['item'] = self.parse_md_table(item)
            l.append(d)
        return l

    def parse_as_tree(self, newest_items: dict) -> dict:
        task_dict = dict()
        def add_task(key, item):
            key_a, key_b = key
            if key_a not in task_dict:
                task_dict[key_a] = {key_b: [item]}
            else:
                if key_b not in task_dict[key_a]:
                    task_dict[key_a][key_b] = [item]
                else:
                    task_dict[key_a][key_b].append(item)

        for item in newest_items:
            project = item['项目名']
            title = item['做了什么']
            task = item['具体任务']
            add_task((project, title), task)

        return task_dict

    # weekly report
    def get_report(self, tree_data: dict) -> str:
        text = []
        for i, (ka, kb) in enumerate(tree_data.items()):
            text.append(f'[{i+1}]{ka}')
            for j, (la, lb) in enumerate(kb.items()):
                text.append(f'    ({j+1}){la}')
                for k, b in enumerate(lb):
                    text.append(f'        {k+1}.{b};')
        return '\n'.join(text)

    def markdown_to_report_text(self) -> str:
        t = []
        for newest_items in self.get_structure_data():
            date_f = '-'.join(newest_items["date_range"])
            t.append(f'## {date_f}')
            tree_data = self.parse_as_tree(newest_items['item'])
            report_text = self.get_report(tree_data)
            t.append(report_text+'\n')
        return '\n'.join(t)

    # year report
    def get_year_report(self, tree_data: dict) -> str:
        text = []
        for i, (ka, kb) in enumerate(tree_data.items()):
            text.append(f'[{i+1}]{ka}')
            for j, (la, lb) in enumerate(kb.items()):
                text.append(f'    ({j+1}){la}')
        return '\n'.join(text)

    def markdown_to_year_report_text(self) -> str:
        xx = []
        for newest_items in self.get_structure_data():
            xx.extend(newest_items['item'])
        tree_data = self.parse_as_tree(xx)
        report_text = self.get_year_report(tree_data)
        return report_text+'\n'

content = read_file('1.表格记录.md')
parser = MarkdownParser(content)
write_file('2.文字记录.md', parser.markdown_to_report_text())
write_file('3.年终总结.md', parser.markdown_to_year_report_text())

Released under the MIT License.