用python 把df全部加边框并输出到execl
时间: 2023-06-06 10:06:31 浏览: 61
好的,这个问题属于技术问题,可以回答。
首先需要安装pandas库和openpyxl库。然后,可以用以下代码将DataFrame中的所有数据加上边框并输出到Excel文件:
```python
import pandas as pd
from openpyxl.styles import Border, Side
from openpyxl.utils.dataframe import dataframe_to_rows
# 读取数据到DataFrame
df = pd.read_csv('data.csv')
# 新建Excel文件
wb = pd.ExcelWriter('output.xlsx')
# 遍历DataFrame中的每个单元格,将其设为有边框
for row in dataframe_to_rows(df, index=False, header=True):
for cell in row:
cell.border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
# 将有边框的DataFrame写入Excel文件
df.to_excel(wb, sheet_name='Sheet1', index=False)
# 保存Excel文件
wb.save()
```
该代码将DataFrame从CSV文件中读取并在Excel文件中输出,每个单元格都有细边框。在输出Excel文件之前,需要确保已安装了pandas库和openpyxl库。