|
| 1 | +#!/bin/env python |
| 2 | +# -*- coding:utf-8 -*- |
| 3 | +# @Author : kaliarch |
| 4 | + |
| 5 | +import xlsxwriter |
| 6 | + |
| 7 | +class create_excle: |
| 8 | + def __init__(self): |
| 9 | + self.tag_list = ["movie_name", "movie_url"] |
| 10 | + self.info = "information" |
| 11 | + |
| 12 | + def create_workbook(self,search=" "): |
| 13 | + excle_name = search + '.xlsx' |
| 14 | + #定义excle名称 |
| 15 | + workbook = xlsxwriter.Workbook(excle_name) |
| 16 | + worksheet_M = workbook.add_worksheet(search) |
| 17 | + worksheet_info = workbook.add_worksheet(self.info) |
| 18 | + print('create %s....' % excle_name) |
| 19 | + return workbook,worksheet_M,worksheet_info |
| 20 | + |
| 21 | + def col_row(self,worksheet): |
| 22 | + worksheet.set_column('A:A', 12) |
| 23 | + worksheet.set_row(0, 17) |
| 24 | + worksheet.set_column('A:A',58) |
| 25 | + worksheet.set_column('B:B', 58) |
| 26 | + |
| 27 | + def shell_format(self,workbook): |
| 28 | + #表头格式 |
| 29 | + merge_format = workbook.add_format({ |
| 30 | + 'bold': 1, |
| 31 | + 'border': 1, |
| 32 | + 'align': 'center', |
| 33 | + 'valign': 'vcenter', |
| 34 | + 'fg_color': '#FAEBD7' |
| 35 | + }) |
| 36 | + #标题格式 |
| 37 | + name_format = workbook.add_format({ |
| 38 | + 'bold': 1, |
| 39 | + 'border': 1, |
| 40 | + 'align': 'center', |
| 41 | + 'valign': 'vcenter', |
| 42 | + 'fg_color': '#E0FFFF' |
| 43 | + }) |
| 44 | + #正文格式 |
| 45 | + normal_format = workbook.add_format({ |
| 46 | + 'align': 'center', |
| 47 | + }) |
| 48 | + return merge_format,name_format,normal_format |
| 49 | + |
| 50 | + #写入title和列名 |
| 51 | + def write_title(self,worksheet,search,merge_format): |
| 52 | + title = search + "搜索结果" |
| 53 | + worksheet.merge_range('A1:B1', title, merge_format) |
| 54 | + print('write title success') |
| 55 | + |
| 56 | + def write_tag(self,worksheet,name_format): |
| 57 | + tag_row = 1 |
| 58 | + tag_col = 0 |
| 59 | + for num in self.tag_list: |
| 60 | + worksheet.write(tag_row,tag_col,num,name_format) |
| 61 | + tag_col += 1 |
| 62 | + print('write tag success') |
| 63 | + |
| 64 | + #写入内容 |
| 65 | + def write_context(self,worksheet,con_dic,normal_format): |
| 66 | + row = 2 |
| 67 | + for k,v in con_dic.items(): |
| 68 | + if row > len(con_dic): |
| 69 | + break |
| 70 | + col = 0 |
| 71 | + worksheet.write(row,col,k,normal_format) |
| 72 | + col+=1 |
| 73 | + worksheet.write(row,col,v,normal_format) |
| 74 | + row+=1 |
| 75 | + print('write context success') |
| 76 | + |
| 77 | + def write_info(self,worksheet_info,info_list,normal_format): |
| 78 | + row = 1 |
| 79 | + for infomsg in info_list: |
| 80 | + for num in range(0,len(infomsg)): |
| 81 | + worksheet_info.write(row,num,infomsg[num],normal_format) |
| 82 | + num += 1 |
| 83 | + row += 1 |
| 84 | + |
| 85 | + print("wirte info success") |
| 86 | + |
| 87 | + |
| 88 | + #关闭excel |
| 89 | + def workbook_close(self,workbook): |
| 90 | + workbook.close() |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + print('This is create excel mode') |
0 commit comments