python openxl border属性
时间: 2023-08-31 12:11:37 浏览: 152
在使用openpyxl库处理Excel表格时,可以使用border属性来设置单元格的边框样式和颜色。首先,需要导入相关的库和模块:
```python
from openpyxl import Workbook
from openpyxl.styles import Border, Side
```
然后,创建一个Workbook对象,并选择活动的工作表:
```python
wb = Workbook()
ws = wb.active
```
接下来,可以通过设置Side对象的属性来定义边框的样式和颜色:
```python
side = Side(
style="medium", # 边框样式,可选dashDot、dashDotDot、dashed、dotted、double、hair、medium、mediumDashDot、mediumDashDotDot、mediumDashed、slantDashDot、thick、thin
color="ff66dd" # 边框颜色,16进制rgb表示
)
```
然后,将边框应用到指定的单元格:
```python
ws\["B2"\].border = Border(
top=side, # 上
bottom=side, # 下
left=side, # 左
right=side, # 右
diagonal=side # 对角线
)
```
最后,保存Excel文件:
```python
wb.save("./test.xlsx")
```
这样就可以使用openpyxl库的border属性来设置单元格的边框样式和颜色了。\[3\]
#### 引用[.reference_title]
- *1* [python3处理excel【三方库—openpyxl】](https://blog.csdn.net/qq_40494873/article/details/119790933)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* *3* [python-- openpyxl详解](https://blog.csdn.net/weixin_44634704/article/details/125098082)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文