0

这是我试图在特定单元格中的现有 excel 表中写入值但值未在该表中打印的部分,如何写入该值,这里我使用 xlutils.copy

from datetime import datetime, timedelta, date
from xlrd import open_workbook
from xlwt import Workbook
from xlutils.copy import copy

import xlrd
import datetime

book = open_workbook('Data.xlsx')
sheet = book.sheet_by_index(0)

# read header values into the list    
keys = [sheet.cell(0, col_index).value for col_index in xrange(sheet.ncols)]

dict_list = []
 #read the excel sheet data into list
 for row_index in xrange(1, sheet.nrows):
     d = {keys[col_index]: sheet.cell(row_index, col_index).value 
     for col_index in xrange(sheet.ncols)}
     dict_list.append(d)
     TotalEffort = 0
   #convert the integer date to YMD format
 for count in range(len(dict_list)):
year, month, day, hour, minute, second =     xlrd.xldate_as_tuple(dict_list[count]["Date"],book.datemode)
   #print week number   
     if datetime.date.today().isocalendar()[1] == date(year, month, day).isocalendar()[1]:      
        TotalEffort = TotalEffort+dict_list[count]["Effort"]
        weeknum = str(datetime.date.today().isocalendar()[1])
        Total = str(TotalEffort)
        print " Effort for week"+weeknum+" is: "+Total+"hours"

rb = open_workbook('output.xlsx')
ws = rb.sheet_by_index(0)
for rowidx in range(ws.nrows):# number of rows in sheets
    row = ws.row(rowidx)# count row from 0 and get it frm sheet
    for colidx, cell in enumerate(row):#read all rows in sheets
        if cell.value == "search word":
           print 'row   ' ,rowidx
           print 'column' ,colidx
           cur_row = rowidx+2
           cur_col = colidx+36
           wb = copy(rb)
           #pic first sheet
           shw = wb.get_sheet(0)
           value = str(Total)
           #writing to shw
           shw.write(cur_row,cur_col,'value')
4

0 回答 0