2

我在 python 中使用 xlrd、xlwt 和 xlutils 库从 excel 表中读取和写入。所以,我已经成功地读取并写入了 excel 表。

但是我需要一些指导来捕获我正在读取的单元格的格式并将相同的格式应用于我正在写入的单元格。

以下是我尝试过的 -

#Open excel sheet & read from tab1
rb = open_workbook(r'C:\Users\abc\excel.xls', formatting_info=True)
sheet = rb.sheet_by_name("tab1")

#Search the excel sheet for the string and return location of Cell
def searchStringCell(searchString):
    for i in range(sheet.nrows):
         row = sheet.row_values(i)
         for j in range(len(row)):
              if row[j] == searchString:
                    return i,j

# Search string to be read
header='<Title>'
headerTitleX,headerTitleY=searchStringCell(header)
cell = sheet.cell(headerTitleX, headerTitleY)
print "Title Cell : (",headerTitleX,",",headerTitleY,")"

#Write data to cell which is 2 columns to the right of the cell already read
wb = copy(rb)
w_sheet = wb.get_sheet(0)
w_sheet.write(headerTitleX, headerTitleY+2, 'New data')

#Have tried the below; not sure about what to do
fmt = rb.xf_list[cell.xf_index]
bg=fmt.background.pattern_colour_index
rgb = wb.colour_map[bg]

#Save the changes
wb.save(r'C:\Users\abc\excel.xls')

我试图从工作表中读取 fmt、bg 和 rgb;但我不确定如何应用读取数据的单元格的相同格式并将其应用于新单元格。谢谢。

4

0 回答 0