在XlsxWriter中写入后,将格式应用于单元格

问题描述 投票:23回答:4

我使用XlsxWriter处理python并且我一直试图解决这个问题但没有成功:

我的应用程序必须创建一个Xlsx文件,其中数据以类似表格的结构显示。该表有一些空单元格。

我想为一些单元格设置边框以为表格制作网格,所以我使用:

format6 = excelbook.add_format()
format6.set_left(1)
for y in range(24):
    excel.write(y+5, 1, None, format6)

为了将边框应用于这些单元格。然后,我在桌子上写数据。

由于表格布局非常复杂,因此编写数据很容易,一旦编写完所有内容,就可以将格式应用于单元格以获得边框,但我无法找到方法。

有没有办法在格式化之前将格式应用于单元格而不会丢失其内容?

先感谢您。

python excel xlsxwriter
4个回答
32
投票

我是该模块的作者,不幸的是,这是不可能的。

它是一个planned feature,内部基础设施的(一小部分)是支持它,但它目前不可用,我不知道它什么时候会出现。


23
投票

另一个解决方法是使用conditional_format,并使用type='no_errors'

worksheet.conditional_format(your_range, {'type': 'no_errors',
                                          'format': your_format})

6
投票

一种方法 - 使用一个包装器方法写入单元格,使用辅助方法来覆盖单元格的值和样式

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()

enter image description here


0
投票

您可以设置工作簿的默认格式:

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')
...
© www.soinside.com 2019 - 2024. All rights reserved.