python で excel ファイルを作成する方法のメモ。
import xlsxwriter class ExcelConverter: def __init__(self, fname): self.workbook = xlsxwriter.Workbook(fname) self.worksheet = self.workbook.add_worksheet() # 赤・ボールド self.strong = self.workbook.add_format({'color': 'red', 'bold': True}) # 上・左詰め・折り返し self.cell_format = self.workbook.add_format() self.cell_format.set_text_wrap() self.cell_format.set_align('left') self.cell_format.set_align('top') self.data_idx = 0 self.row = 0 # ヘッダ行を出力する def output_header(self): self.row += 1 cell = f'A{self.row}' self.worksheet.write(cell, '行番号') cell = f'B{self.row}' self.worksheet.write(cell, 'データ') # データを出力する def output_data(self, obj): self.data_idx += 1 self.row += 1 # 行番号 cell = f'A{self.row}' self.worksheet.set_column(f'{cell}:{cell}', 7, self.cell_format) self.worksheet.write(cell, self.data_idx) # データ cell = f'B{self.row}' self.worksheet.set_column(f'{cell}:{cell}', 15, self.cell_format) self.worksheet.write(cell, obj['product-id']) def close(self): self.workbook.close() def main(): out_fname = sys.argv[1] conv = ExcelConverter(out_fname) conv.output_header() for line in sys.stdin: try: obj = json.loads(line) conv.output_data(obj) except Exception as e: try: sys.stderr.write(json.dumps(obj, ensure_ascii=False) + '\n') sys.stderr.write(e) sys.stderr.write('\n') except: pass conv.close()