with pd.ExcelWriter(f"xxx.xlsx", engine="openpyxl", date_format="YYYY-MM-DD", datetime_format="YYYY-MM-DD HH:MM:SS" ) as writer: df.to_excel(writer) sheet = writer.sheets["Sheet1"] # 设置列宽 sheet.column_dimensions['A'].width = 12.5 # 设置行高 sheet.row_dimensions[1].height = 72 # 设置表头(第一行)自动换行 r = sheet[1] for c in r: c.alignment = openpyxl.styles.Alignment(wrapText=True) # 设置表体数字格式显示4位小数 for i, r in enumerate(sheet): for j, c in enumerate(r): if i != 0 and j != 0: c.number_format = "0.0000"