Python Xlrd and Xlwt

一笑奈何 提交于 2019-12-04 20:53:56

Try the following. This uses the xlrd and xlwt libraries to read and write xls spreadsheets:

import xlrd
import xlwt

wb_in = xlrd.open_workbook(r'input.xls')
sheet_name = wb_in.sheet_names()[0]
ws_in = wb_in.sheet_by_name(sheet_name)

wb_out = xlwt.Workbook()
ws_out = wb_out.add_sheet(sheet_name)   # Use the same sheet name

row_out = 0

for row_in in range(ws_in.nrows):
    row = ws_in.row_values(row_in)

    if isinstance(row[2], float):
        req_spec = str(int(row[2]))
    else:
        req_spec = row[2]

    req_range = req_spec.split('-')
    req_enum = req_spec.split(',')

    if len(req_range) > 1:    # e.g. 10010-10040-10
        for value in range(int(str(req_range[0])), int(str(req_range[1])) + 1, int(str(req_range[2]))):
            ws_out.write(row_out, 0, row[0])
            ws_out.write(row_out, 1, row[1])
            ws_out.write(row_out, 2, str(value))
            row_out += 1
    elif len(req_enum) > 1:    # e.g. 1010,1020
        for value in req_enum:
            ws_out.write(row_out, 0, row[0])
            ws_out.write(row_out, 1, row[1])
            ws_out.write(row_out, 2, value)
            row_out += 1
    else:                      # e.g. 10100
        ws_out.write(row_out, 0, row[0])
        ws_out.write(row_out, 1, row[1])
        ws_out.write(row_out, 2, req_spec)
        row_out += 1

wb_out.save('output.xls')  

Unfortunately, if you not familiar with Python there is quite a lot to take in.

The script works by creating an input workbook and an output workbook. For each row in the input, it assumes you will always have 3 columns and that the third one contains one of your three types of specifies. It decides which is in use based on whether or not there is a - or a , present. It then writes out rows to the output based on this range.

Note, when reading the file in, xlrd attempts to guess the format of the cell. For most of your entries, it guesses a string format, but sometimes it wrongly guesses a floating point number. The script tests for this and converts it to a string for consistency. Also xlrd uses unicode u"xxx" format to store the strings. These need to be converted to numbers to be able to calculate the required ranges.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!