xlsxwriter worksheet.write_blank适用于范围

时间:2018-06-22 20:23:09

标签: python excel format xlsxwriter

我正在尝试使用xlsxwriter向一系列单元格写入格式。

ggplot(a, aes(x=Week, y=Volume, group=ChartGroup, color=ChartGroup)) + 
  geom_line() + 
  geom_point() + 
  facet_grid(facet~.,scales='free_y') +
  theme(axis.text.x = element_text(angle = 90)) +
  labs(title="Volume") + 
  labs(x= "Week") + 
  labs(y ="Volume") +
  scale_x_date(breaks = seq(as.Date(lims[1]), as.Date(lims[2]), by="1 week"), date_labels = "%Y-%m-%d")

此write_blank方法似乎仅适用于单个单元格引用,因为它的格式为“ A4”,而不是“ B4”。有没有一种方法可以将这种格式应用于单元格范围而无需向单元格写入值?我想更改设置的excel范围内所有单元格的颜色。

1 个答案:

答案 0 :(得分:1)

尝试此功能:

import string

# function to write blank format to range of cells
def range_write_blank(worksheet, col_start, col_end, row_start, row_end, cel_format):
    '''
    Writes blank to range of cells cel_format
    Takes:
    worksheet as Worksheet object
    col_start as uppercase letter string
    col_end as uppercase letter string
    row_start as int
    row_end as int
    cel_format as XlsXWriter Format object
    '''
    letters = list(string.ascii_uppercase)
    for r in range(row_start, row_end):
        for c in range(letters.index(col_start), letters.index(col_end)+1):
            worksheet.write_blank(r, c, '', cel_format)

# usage example
some_format = workbook.add_format({
    'bold': 1,
    'border': 1,
    'align': 'center',
    'valign': 'vcenter',
    'fg_color': 'blue'})

range_write_blank(worksheet, "A", "C", 0, 10, some_format)