python 设置第二张sheet的格式(边框)
时间: 2023-08-26 18:26:27 浏览: 125
你可以使用Python中的openpyxl模块来设置Excel文件中的格式,以下是设置第二张sheet的边框格式的示例代码:
```python
import openpyxl
from openpyxl.styles import Border, Side
# 打开Excel文件
wb = openpyxl.load_workbook('example.xlsx')
# 获取第二张Sheet
sheet = wb.worksheets[1]
# 设置边框样式
border_style = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
# 遍历每个单元格并设置边框
for row in sheet.iter_rows():
for cell in row:
cell.border = border_style
# 保存Excel文件
wb.save('example.xlsx')
```
在这个示例中,我们使用了openpyxl模块的Border和Side类来创建边框样式。我们定义了一个包含四个边框的样式,并使用`iter_rows()`方法遍历每个单元格并将边框样式应用到它们上面。最后,我们使用`save()`方法保存Excel文件。
相关问题
使用python代码 pandas库 实现从excel"resource.xlsx"的"sheet1"中不设立表头的读取第9行以下的所有数据,从另一个excel"result.xlsm"中的"sheet2"的第10行开始,保留sheet2的格式不设立表头的写入这些数据,对新添加的数据填充补sheet2的源格式及添加边框 所有代码
好的,以下是你需要的代码:
```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"的格式不设立表头。然后,它会填充新添加的数据的格式,并添加边框。最后,它会保存文件。
用python将新建的df按两列分组,两个列名分别为表名和sheeet名,把分组后的df按顺序输出到已有excel中的倒数第二个sheet,表头填充为浅蓝色,全表加边框
您好,可以使用 pandas 中的 groupby 函数来按照两个指定列进行分组,然后将分组后的数据写入已有的 Excel 中。具体操作可以按照下面的代码实现:
```python
import pandas as pd
import openpyxl
from openpyxl.styles import Color, Font, Border, Side
from openpyxl.utils import get_column_letter
# 读取已有的 Excel 文件
book = openpyxl.load_workbook('example.xlsx')
writer = pd.ExcelWriter('example.xlsx', engine='openpyxl')
writer.book = book
# 准备数据
df = pd.DataFrame({'表名': ['表1', '表1', '表2', '表2'],
'sheet名': ['sheet1', 'sheet1', 'sheet2', 'sheet2'],
'数据': [1, 2, 3, 4]})
# 按照表名和sheet名进行分组
groups = df.groupby(['表名', 'sheet名'])
# 将分组后的数据写入 Excel 文件中的倒数第二个 sheet
sheet_name = writer.book.sheetnames[-2]
for i, (name, group) in enumerate(groups):
group.to_excel(writer, sheet_name=sheet_name, startrow=i*len(group.columns)+1, header=False, index=False)
# 给表头加上浅蓝色填充和边框
worksheet = writer.sheets[sheet_name]
header_row = i*len(group.columns)+1
side = Side(border_style='thin', color='000000')
for j, col in enumerate(group.columns):
cell = worksheet.cell(row=header_row, column=j+1)
cell.value = col
cell.fill = openpyxl.styles.fills.PatternFill(patternType='solid', fgColor=Color(rgb='BEEEFF'))
cell.border = Border(top=side, bottom=side, left=side, right=side)
# 保存 Excel 文件
writer.save()
```
在这个例子中,我们首先读取了一个已有的 Excel 文件,并且准备好了一个 DataFrame,其中包含了表名、sheet名和数据列。然后,我们使用 pandas 中的 groupby 函数按照表名和sheet名进行分组。接着,我们将分组后的数据写入已有的 Excel 文件中的倒数第二个 sheet,并且给表头加上了浅蓝色填充和边框。最后,我们保存 Excel 文件并退出。
注意,这个例子中我们使用了 openpyxl 库来对 Excel 文件进行读写操作,并且使用了 openpyxl 中的一些 API 来设置单元格的样式。如果您还没有安装 openpyxl 库,可以使用 pip 命令进行安装:
```bash
pip install openpyxl
```
阅读全文
相关推荐













