简体   繁体   English

Python Xlrd 和 Xlwt

[英]Python Xlrd and Xlwt

I have the following content in an old Excel sheet:我在旧的 Excel 工作表中有以下内容:

excel表格截图

I need to generate a new Excel sheet with the following values:我需要使用以下值生成一个新的 Excel 工作表:

excel表格已更新

In the input Excel file's 3rd column I have a range 10010-10040 and an increment value of 10 .在输入 Excel 文件的第 3 列中,我的范围为10010-10040 ,增量值为10 This needs to be expanded in my new Excel file.这需要在我的新 Excel 文件中展开。

If I give a comma (,) in between values, it should be treated as separate values and expanded.如果我在值之间使用逗号 (,),则应将其视为单独的值并进行扩展。 (Like in row2, column 3) (如第 2 行,第 3 列)

I have no idea how to do this and I am new to Python.我不知道该怎么做,而且我是 Python 新手。

Try the following.请尝试以下操作。 This uses the xlrd and xlwt libraries to read and write xls spreadsheets:这使用xlrdxlwt库来读取和写入xls电子表格:

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.不幸的是,如果您不熟悉 Python,那么有很多东西需要学习。

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.对于输入中的每一行,它假定您将始终有 3 列,并且第三列包含您的三种指定类型中的一种。 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.请注意,在读入文件时, xlrd尝试猜测单元格的格式。 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. xlrd使用 unicode u"xxx"格式来存储字符串。 These need to be converted to numbers to be able to calculate the required ranges.这些需要转换为数字才能计算所需的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM