0

我有一个 excel 表,我正在尝试将数据写入其中。我的工作表用作模板,写完后我将用另一个名称保存文件。我用于填充的python代码是这样的

# -*- coding: utf-8 -*-
import json
import xlrd
from xlutils.copy import copy
import os

book = xlrd.open_workbook('file.xls',formatting_info=True)
wb = copy(book)
sheet1=wb.get_sheet(0)

def _getOutCell(outSheet, colIndex, rowIndex):
    """ HACK: Extract the internal xlwt cell representation. """
    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. """
    # HACK to retain cell style.
    previousCell = _getOutCell(outSheet, col, row)
    # END HACK, PART I

    outSheet.write(row, col, value)

    # HACK, PART II
    if previousCell:
        newCell = _getOutCell(outSheet, col, row)
        if newCell:
            newCell.xf_idx = previousCell.xf_idx
    # END HACK

# Populate the data 
setOutSheet(sheet1,2,3,value)


# Save the file
wb.save("test.xls")

执行代码后,我的 excel 表中的边框被扭曲,数据没有正确填充。我发现纸张在行副本(书)本身上被损坏,任何人都可以帮助解决这个问题。这是获取输出图像后的屏幕截图链接

4

0 回答 0