1
def _getOutCell(outSheet, colIndex, rowIndex):
    row = outSheet._Worksheet__rows.get(rowIndex)
    if not row: return None
    cell = row._Row__cells.get(colIndex)
    return cell

def setOutCell(outSheet, col, row, value):
    """ Change cell value without changing formatting. """
    previousCell = _getOutCell(outSheet, col, row)
    outSheet.write(row, col, value)
    if previousCell:
        newCell = _getOutCell(outSheet, col, row)
        if newCell:
            newCell.xf_idx = previousCell.xf_idx

rs_template_input = xlrd.open_workbook(<path to file>, formatting_info=True)

rs_copy_1 = xlutils.copy.copy(rs_template_input)
rs_copy_2 = xlutils.copy.copy(rs_template_input)

rs_template_sheet_1 = rs_copy_1.get_sheet(0)
rs_template_sheet_2 = rs_copy_2.get_sheet(0)

setOutCell(rs_template_sheet_1, 2, 3, 'project 1')
setOutCell(rs_template_sheet_2, 2, 3, 'project 2')

我希望rs_template_sheet_1rs_template_sheet_2成为一个 excel 文件中的两个工作表/选项卡并保留格式

PS:函数setOutCell用于写入excel中的单元格并保留格式

4

0 回答 0