반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- array
- list
- gas
- 파이썬
- matplotlib
- math
- Tkinter
- Python
- Java
- google apps script
- Mac
- GIT
- PostgreSQL
- PANDAS
- Excel
- PySpark
- Github
- Google Spreadsheet
- string
- SQL
- hive
- Redshift
- django
- numpy
- c#
- Kotlin
- Apache
- dataframe
- Google Excel
Archives
- Today
- Total
달나라 노트
Python Pandas & openpyxl : fill (셀에 색상 채우기) 본문
728x90
반응형
cell 객체의 fill 옵션을 조절하면 셀에 색상을 채울 수 있습니다.
import pandas as pd
from openpyxl.styles import PatternFill
dict_test = {
'col1': [1, 2, 3, 4, 5],
'col2': ['a', 'b', 'c', 'd', 'e'],
'col3': [1234, 0.27383720, 39372, None, 102947291.293472],
'col4': [0.9, 0.5238, 0.13, 0.0028, 1024.29278],
}
df_test = pd.DataFrame(dict_test)
xlsx_writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
df_test.to_excel(xlsx_writer, sheet_name='test', index=False)
worksheet = xlsx_writer.sheets['test']
cell = worksheet['B3'] # B3 cell의 객체를 가져옴
cell.fill = PatternFill(start_color='ffffd2d2', end_color='ffffd2d2', fill_type='solid') # cell 객체에 색을 채움
xlsx_writer.close()
위 코드의 결과는 다음과 같습니다.

B3 cell에 원하는 색상이 채워진 것을 볼 수 있습니다.
Cell에 채울 색상을 의미하는 code는 ARGB code입니다.
GradientFill을 이용하면 아래 코드의 결과처럼 하나의 cell에 그라데이션으로 색을 채울 수도 있습니다.
import pandas as pd
from openpyxl.styles import GradientFill
dict_test = {
'col1': [1, 2, 3, 4, 5],
'col2': ['a', 'b', 'c', 'd', 'e'],
'col3': [1234, 0.27383720, 39372, None, 102947291.293472],
'col4': [0.9, 0.5238, 0.13, 0.0028, 1024.29278],
}
df_test = pd.DataFrame(dict_test)
xlsx_writer = pd.ExcelWriter('test.xlsx', engine='openpyxl')
df_test.to_excel(xlsx_writer, sheet_name='test', index=False)
worksheet = xlsx_writer.sheets['test']
cell = worksheet['B3'] # B3 cell의 객체를 가져옴
cell.fill = GradientFill(stop=('ffffd2d2', 'ffcfe3ff')) # cell 객체에 색을 채움
xlsx_writer.close()

728x90
반응형
'Python > Python Pandas' 카테고리의 다른 글
Python Pandas & openpyxl : columns, rows (column에 대한 객체 얻어오기, row에 대한 객체 얻어오기) (0) | 2024.02.26 |
---|---|
Python Pandas & openpyxl : font (글자 서식 설정) (2) | 2024.02.26 |
Python Pandas & openpyxl : border (셀의 테두리 설정) (0) | 2024.02.26 |
Python Pandas & openpyxl : number_format (숫자의 format 정하기) (0) | 2024.02.26 |
Python Pandas & openpyxl : cell object (cell 객체) (0) | 2024.02.26 |