Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
I can't discover examples where xlwt is utilized to compose into existing documents. I have a current Xls document that I need to write to. At the point when I use xlrd to peruse the document, I cannot appear to sort out some way to change the "Book" type returned into a xlwt.Workbook. I would value it in the event that somebody can direct me towards an example.

1 Answer

0 votes
by (26.4k points)

Here's some example code I utilized as of late to do exactly that. 

It opens an exercise manual, goes down the rows, if a condition is met it keeps in touch with some information in the line. At long last, it saves the altered file.

from xlutils.copy import copy # http://pypi.python.org/pypi/xlutils

from xlrd import open_workbook # http://pypi.python.org/pypi/xlrd

START_ROW = 297 # 0 based (subtract 1 from excel row number)

col_age_november = 1

col_summer1 = 2

col_fall1 = 3

rb = open_workbook(file_path,formatting_info=True)

r_sheet = rb.sheet_by_index(0) # read only copy to introspect the file

wb = copy(rb) # a writable copy (I can't read values out of this, only write to it)

w_sheet = wb.get_sheet(0) # the sheet to write to within the writable copy

for row_index in range(START_ROW, r_sheet.nrows):

    age_nov = r_sheet.cell(row_index, col_age_november).value

    if age_nov == 3:

        #If 3, then Combo I 3-4 year old  for both summer1 and fall1

        w_sheet.write(row_index, col_summer1, 'Combo I 3-4 year old')

        w_sheet.write(row_index, col_fall1, 'Combo I 3-4 year old')

wb.save(file_path + '.out' + os.path.splitext(file_path)[-1])

Want to learn python to get expertise in the concepts of python? Join python certification course and get certified

Related questions

0 votes
0 answers
0 votes
1 answer
0 votes
1 answer
asked Feb 18, 2021 in Python by adhiraj (4k points)

Browse Categories

...