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 を読み取ろうとしました。しかし、データを読み取って新しいセルに適用するセルと同じ形式を適用する方法がわかりません。ありがとう。