通过openpyxl从json创建xlsx。
xlsx-from-json的Python项目详细描述
xlsx_来自_json
通过openpyxl从json创建xlsx。
用法
创建具有以下结构的.json文件:
{"rows":[{"cells":[{"value":"Sample text",// merge 5x2 cell range"width":5,"height":2,// openpyxl style: https://openpyxl.readthedocs.io/en/2.5/styles.html"style":{"font":{"name":"Times New Roman","size":12},"border":{"bottom":{// openpyxl border styles: // https://openpyxl.readthedocs.io/en/stable/_modules/openpyxl/styles/borders.html"border_style":"medium","color":"FFFFFFFF"}}}}],// move row by 2x1"skip_columns":2,"skip_rows":1,// change row height"row_height":10}],"start_column":1,"start_row":1,// change column widths"column_widths":[{// column_number or column_letter"column_number":1,"width":10}],// set number format (e.g. 1.234 -> 1.23)"number_format":"0.00",// apply style to all cells"default_style":{"font":{"bold":true}}}
通过xlsx_from_json
函数创建openpyxl工作簿:
importjsonfromxlsx_from_jsonimportxlsx_from_jsonwithopen("data.json",encoding="utf-8")asf:json_data=json.load(f)wb=xlsx_from_json(json_data)
创建的工作簿将具有以下值:
sheet=wb.activeassertsheet.cell(row=2,cell=3).value=="Sample text"
样式也一样(单元格样式+默认样式):
sheet=wb.activeassertsheet.cell(row=2,cell=3).font.name=="Times New Roman"assertsheet.cell(row=2,cell=3).font.bold==True
现在您可以根据openpyxlguide使用工作簿。