Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

求在xls中自动生成多行数据的方法

image
以上是我的案卷级目录表,里面每一行有汇总的起止案卷号,我想在第二个工作表中根据第一个工作表的这个起止号自动生成对应条数的案卷级数据,如下表:
image
image
如果汇总的起止案卷号目录表的数据量少到还好办,但如果这个表的数据量有1000条以上,如果用原始的办法就太费事了,各位大神有什么更好更快捷的方法,求指导!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我写了个demo,你根据实际情况做下更改

import xlrd
import xlwt

if __name__ == "__main__":
    workbook = xlrd.open_workbook('test.xlsx')
    target = xlwt.Workbook(encoding='utf-8')
    table = workbook.sheet_by_index(0)
    rows = table.nrows
    for row in range(rows):
        if (row == 0):
            continue
        row_data = table.row_values(row)
        name = row_data[0]
        [start, end] = row_data[1].split("-")
        sheet = target.add_sheet(name)
        sheet.write(0, 0, '名称')
        sheet.write(0, 1, '案卷号')
        for i in range(int(end) - int(start)):
            sheet.write(1 + int(i), 0, name)
            sheet.write(1 + int(i), 1, 1 + i + int(start))
    target.save("target.xlsx")

test.xlsx
38201692-97B6-4345-ABD5-B0F4B56E265E.png
target.xlsx
F089653A-D11B-4af8-A428-7EE7E715F08F.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...