0

Here is the piece I am trying to write the value inside existing excel sheet in particular cell but value is not printing inside that sheet,how to write that value,here I used 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')
doubts
  • 157
  • 1
  • 1
  • 9
  • From what I see, the script never comes to the write action as `value=str(Total)` will result in an exception: `NameError: name 'Total' is not defined`. Are your print statements executed? (Remark: printing using `"...",...` creates a new string out of the two given ones in python 2.7 - i personally prefer using "...%s"%var to print them to avoid using much memory for that) Please post only (!) running code, in that case import section is missing – R4PH43L Sep 11 '15 at 05:52
  • When are you closing ( finalizing ) your worksheets? Shouldn't it be `shw.write(cur_row, cur_col, value)`? as value is a string and should be replaced and not written 'value' as a literal - or do i get anything wrong here? – R4PH43L Sep 15 '15 at 05:41
  • Based on your current code there is now way i can help you as i get your minimal data as "3 columns ("Date", "Effort", "search word")" with headers in the first row and data in all columns but "search word". I do not get how you want to parse your data from the code. ` d = {keys[col_index]: sheet.cell(row_index, col_index).value ` will not work as you are initializing `col_index` in the iteration row below. Please fix your indentation and your code. Then I might help you with it. – R4PH43L Sep 15 '15 at 06:12

0 Answers0