阅读excel cell&写入另一个在python xlrd / xlwt中应用相同格式的excel表

时间:2015-03-16 15:01:02

标签: python excel xlrd xlwt xlutils

我正在使用xlrd,xlwt& python中的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&amp; rgb来自表格;但我不确定如何应用从中读取数据的相同格式的单元格&amp;将其应用于新单元格。谢谢。

0 个答案:

没有答案