1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import pandas as pd from datetime import datetime, timedelta
def modify_excel_format(excel_data, writer, df): workbook = writer.book fmt = workbook.add_format({"font_name": u"宋体"}) col_fmt = workbook.add_format( {'bold': True, 'font_size': 11, 'font_name': u'宋体', 'border': 1, 'bg_color': '#0265CB','font_color': 'white', 'valign': 'vcenter', 'align': 'center'}) detail_fmt = workbook.add_format( {"font_name": u"宋体", 'border': 0, 'valign': 'vcenter', 'align': 'center','font_size': 11, 'text_wrap': True}) worksheet1 = writer.sheets['Sheet1'] for col_num, value in enumerate(df.columns.values): worksheet1.write(0, col_num, value, col_fmt) worksheet1.set_column('A:F', 20, fmt) worksheet1.set_row(0, 30, fmt) for i in range(1, len(excel_data)+1): worksheet1.set_row(i, 27, detail_fmt)
excel_data = [] columns = ["用户", "名字", "标题", "类", "测试", "语言"] tmp = ["张同学", "张同学", "pd 生成excel", "pandas", "pd", "python"] excel_data.append(tmp) df = pd.DataFrame(data=excel_data, columns=columns) t = datetime.now().date() - timedelta(days=1) with pd.ExcelWriter(path='demo-%d%02d%02d.xlsx' % (t.year, t.month, t.day), engine="xlsxwriter") as writer: df.to_excel(writer, sheet_name='Sheet1', encoding='utf8', header=False, index=False, startcol=0, startrow=1) modify_excel_format(excel_data, writer, df) writer.save() print('ok!')
|