使用python代码 pandas库 实现从excel"resource.xlsx"的"sheet1"中不设立表头的读取第9行以下的所有数据,从另一个excel"result.xlsm"中的"sheet2"的第10行开始,保留sheet2的格式不设立表头的写入这些数据,对新添加的数据填充补sheet2的源格式及添加边框 所有代码
时间: 2024-02-19 17:03:26 浏览: 74
pandas读取xlsx文件.md
好的,以下是你需要的代码:
```python
import pandas as pd
from openpyxl import load_workbook
from openpyxl.styles.borders import Border, Side
# 读取resource.xlsx中sheet1的数据
df = pd.read_excel('resource.xlsx', sheet_name='sheet1', header=None, skiprows=8)
# 读取result.xlsm中sheet2的数据
book = load_workbook('result.xlsm')
writer = pd.ExcelWriter('result.xlsm', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df.to_excel(writer, sheet_name='sheet2', header=None, startrow=9)
# 填充新添加的数据的格式
ws = book['sheet2']
border = Border(left=Side(border_style='thin', color='000000'),
right=Side(border_style='thin', color='000000'),
top=Side(border_style='thin', color='000000'),
bottom=Side(border_style='thin', color='000000'))
for row in range(10, ws.max_row+1):
for col in range(1, ws.max_column+1):
cell = ws.cell(row=row, column=col)
cell.border = border
# 保存文件
writer.save()
```
这段代码会从"resource.xlsx"的"sheet1"中读取第9行以下的所有数据,并将其写入到"result.xlsm"的"sheet2"中的第10行开始,保留"sheet2"的格式不设立表头。然后,它会填充新添加的数据的格式,并添加边框。最后,它会保存文件。
阅读全文