반응형
XlsxWriter로 쓴 후 셀에 형식 적용
저는 XlsxWriter를 사용하여 python에서 일하고 있으며, 이 문제를 해결하려고 노력했지만 성공하지 못했습니다.
제 앱은 Xlsx 파일을 만들어야 합니다.이 파일에는 데이터가 테이블 형태로 표시됩니다.저 테이블에는 빈 셀이 몇 개 있다.
테이블의 그리드를 만들기 위해 일부 셀에 테두리를 설정하여 다음을 사용합니다.
format6 = excelbook.add_format()
format6.set_left(1)
for y in range(24):
excel.write(y+5, 1, None, format6)
그 세포들에 테두리를 적용하기 위해서요그리고 나서, 저는 테이블에 데이터를 씁니다.
표의 레이아웃이 매우 복잡하기 때문에 데이터를 쓰기 쉽고, 모든 것을 쓴 후에는 셀에 포맷을 적용하여 테두리를 만듭니다만, 방법이 없습니다.
내용을 잃지 않고 미리 써놓은 셀에 포맷을 적용할 수 있는 방법이 있나요?
잘 부탁드립니다.
저는 그 모듈의 저자입니다만, 유감스럽게도 그것은 불가능합니다.
이 기능은 계획된 기능으로 내부 인프라스트럭처의 일부가 지원되고 있지만, 현재 사용할 수 없기 때문에 언제 제공될지는 알 수 없습니다.
업데이트: 이 기능은 구현되지 않았으며 계획되지 않았습니다.
또 다른 회피책은conditional_format
, 및 사용type='no_errors'
:
worksheet.conditional_format(your_range, {'type': 'no_errors',
'format': your_format})
한 가지 방법 - 하나의 래퍼 방법을 사용하여 셀을 쓰고 도우미 방법을 사용하여 셀의 값과 스타일을 덮어씁니다.
import xlsxwriter
class XLSGenerator:
def __init__(self):
self.workbook = xlsxwriter.Workbook('file.xls')
sheet1 = self.workbook.add_worksheet('sheet1')
sheet2 = self.workbook.add_worksheet('sheet2')
self.sheets = {'sheet1': sheet1, 'sheet2': sheet2}
# dictionary with all written cells
self.written_cells = {sheet: {} for sheet in self.sheets}
def write_cell(self, sheet_name, cell, value, cell_format_dict=None):
"""Writes value and style, and saves it in self.written_cells"""
sheet = self.sheets[sheet_name]
if cell_format_dict:
cell_format = self.workbook.add_format(cell_format_dict)
sheet.write(cell, value, cell_format)
else:
cell_format_dict = None
sheet.write(cell, value)
# save sheet_name, cell and cell_value, and cell_format (dict)
# example ['sheet1']['C12'] = ('some_text', {'font_size': 14, 'bold': True}
self.written_cells[sheet_name][cell] = (value, cell_format_dict)
def apply_style(self, sheet_name, cell, cell_format_dict):
"""Apply style for any cell, with value or not. Overwrites cell with joined
cell_format_dict and existing format and with existing or blank value"""
written_cell_data = self.written_cells[sheet_name].get(cell)
if written_cell_data:
existing_value, existing_cell_format_dict = self.written_cells[sheet_name][cell]
updated_format = dict(existing_cell_format_dict or {}, **cell_format_dict)
else:
existing_value = None
updated_format = cell_format_dict
self.write_cell(sheet_name, cell, existing_value, updated_format)
이런 사용법
generator = XLSGenerator()
generator.write_cell('sheet1', 'A1', '10')
generator.write_cell('sheet1', 'B2', '20')
generator.write_cell('sheet1', 'C3', '30')
table_borders = {"left": 1, 'right': 1, 'top': 1, 'bottom': 1}
for cell in ('A1', 'A2', 'A3', 'B1', 'B2', 'B3', 'C1', 'C2', 'C3'):
generator.apply_style('sheet1', cell, table_borders)
generator.workbook.close()
워크북의 기본 형식을 설정할 수 있습니다.
import xlsxwriter
workbook = xlsxwriter.Workbook('example.xlsx')
# default cell format to size 10
workbook.formats[0].set_font_size(10)
# default cell format to center
workbook.formats[0].set_align('center')
...
언급URL : https://stackoverflow.com/questions/22352907/apply-format-to-a-cell-after-being-written-in-xlsxwriter
반응형
'programing' 카테고리의 다른 글
콘솔을 클리어하려면 (0) | 2023.04.16 |
---|---|
셸 스크립트의 행 끝에 세미콜론이 필요합니까? (0) | 2023.04.11 |
x64 사용자 지정 클래스의 각 열거에 대한 버그 (0) | 2023.04.11 |
VB 코드에서 VBProject 보호 해제 (0) | 2023.04.11 |
Bash에서 명령줄 인수를 변경하려면 어떻게 해야 합니까? (0) | 2023.04.11 |