|
2 | 2 | import sys
|
3 | 3 | import json
|
4 | 4 |
|
| 5 | +import openpyxl |
| 6 | +from openpyxl.styles import Font, PatternFill |
5 | 7 | import click
|
6 | 8 | from prettytable import PrettyTable
|
7 | 9 | from simple_loggers import SimpleLogger
|
|
19 | 21 | nsfc query -C # 输出数量
|
20 | 22 | nsfc query -C -s approval_year 2019 # 按批准年份查询
|
21 | 23 | nsfc query -C -s approval_year 2019 -s subject_code "%A%" # 按批准年份+学科代码(模糊)
|
22 |
| - nsfc query -C -s approval_year 2015-2019 -s subject_code "%C01%" # 批准年份可以是一个区间 |
| 24 | + nsfc query -C -s approval_year 2015-2019 -s subject_code "%C01%" # 批准年份也可以是一个区间 |
23 | 25 | nsfc query -o A.2019.jl -s approval_year 2019 -s subject_code "%A%" # 结果输出为.jl文件
|
24 | 26 | nsfc query -F tsv -o A.2019.tsv -s approval_year 2019 -s subject_code "%A%" # 结果输出为tsv文件
|
25 | 27 | nsfc query -L 5 -s approval_year 2019 # 限制最大输出条数
|
|
36 | 38 | @click.option('-o', '--outfile', help='the output filename')
|
37 | 39 |
|
38 | 40 | @click.option('-F', '--format', help='the format of output',
|
39 |
| - type=click.Choice(['json', 'jl', 'tsv']), default='jl', |
| 41 | + type=click.Choice(['json', 'jl', 'tsv', 'xlsx']), default='jl', |
40 | 42 | show_choices=True, show_default=True)
|
41 | 43 | @click.option('-K', '--keys', help='list the available keys for query', is_flag=True)
|
42 | 44 | @click.option('-C', '--count', help='just output the out of searching', is_flag=True)
|
@@ -101,22 +103,42 @@ def main(**kwargs):
|
101 | 103 | elif not query.count():
|
102 | 104 | logger.warning('no result for your input')
|
103 | 105 | else:
|
104 |
| - out = open(outfile, 'w') if outfile else sys.stdout |
105 |
| - with out: |
106 |
| - if kwargs['format'] == 'json': |
107 |
| - data = [{k: v for k, v in row.__dict__.items() if k != '_sa_instance_state'} for row in query] |
108 |
| - out.write(json.dumps(data, ensure_ascii=False, indent=2) + '\n') |
109 |
| - else: |
110 |
| - for n, row in enumerate(query): |
111 |
| - context = {k: v for k, v in row.__dict__.items() if k != '_sa_instance_state'} |
112 |
| - if n == 0 and kwargs['format'] == 'tsv': |
113 |
| - title = '\t'.join(context.keys()) |
114 |
| - out.write(title + '\n') |
115 |
| - if kwargs['format'] == 'tsv': |
116 |
| - line = '\t'.join(map(str, context.values())) |
117 |
| - else: |
118 |
| - line = json.dumps(context, ensure_ascii=False) |
119 |
| - out.write(line + '\n') |
| 106 | + if outfile and kwargs['format'] == 'xlsx': |
| 107 | + wb = openpyxl.Workbook() |
| 108 | + ws = wb.active |
| 109 | + ws.title = 'NSFC-RESULT' |
| 110 | + title = [k for k, v in query.first().__dict__.items() if k != '_sa_instance_state'] |
| 111 | + ws.append(title) |
| 112 | + for col, v in enumerate(title,1 ): |
| 113 | + _ = ws.cell(row=1, column=col, value=v) |
| 114 | + _.font = Font(color='FFFFFF', bold=True) |
| 115 | + _.fill = PatternFill(start_color='000000', end_color='000000', fill_type='solid') |
| 116 | + |
| 117 | + for n, row in enumerate(query): |
| 118 | + context = [v for k, v in row.__dict__.items() if k != '_sa_instance_state'] |
| 119 | + ws.append(context) |
| 120 | + |
| 121 | + ws.freeze_panes = 'A2' |
| 122 | + wb.save(outfile) |
| 123 | + else: |
| 124 | + out = open(outfile, 'w') if outfile else sys.stdout |
| 125 | + with out: |
| 126 | + if kwargs['format'] == 'json': |
| 127 | + data = [{k: v for k, v in row.__dict__.items() if k != '_sa_instance_state'} for row in query] |
| 128 | + out.write(json.dumps(data, ensure_ascii=False, indent=2) + '\n') |
| 129 | + else: |
| 130 | + for n, row in enumerate(query): |
| 131 | + context = {k: v for k, v in row.__dict__.items() if k != '_sa_instance_state'} |
| 132 | + if n == 0 and kwargs['format'] == 'tsv': |
| 133 | + title = '\t'.join(context.keys()) |
| 134 | + out.write(title + '\n') |
| 135 | + if kwargs['format'] == 'tsv': |
| 136 | + line = '\t'.join(map(str, context.values())) |
| 137 | + else: |
| 138 | + line = json.dumps(context, ensure_ascii=False) |
| 139 | + out.write(line + '\n') |
| 140 | + if outfile: |
| 141 | + logger.info(f'save file: {outfile}') |
120 | 142 |
|
121 | 143 |
|
122 | 144 | if __name__ == '__main__':
|
|
0 commit comments