前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >100行代码可以干点啥

100行代码可以干点啥

作者头像
编程随想曲
发布2022-12-01 16:28:12
1770
发布2022-12-01 16:28:12
举报
文章被收录于专栏:编程随想曲编程随想曲

一个想法:

Python实现将一个Excel表中的每一行提取出来形成单独的Excel文件。

吭呲瘪肚:

代码语言:javascript
复制
# 实现逻辑
# 读取总表,循环逐行读取。
# 读一行,通过模板复制一个文件,写入信息。
# 读不到了,退出循环。
import xlrd  # 导入库
import xlwt
import os
from shutil import copyfile
from xlutils.copy import copy

xlsx = xlrd.open_workbook('/Users/luoxiaolei/Desktop/井盖/总表.xls')
sheet = xlsx.sheets()[0]

initRowNum = 2
collMinNum = 0
collMaxNum = 23
xuhao =  str(sheet.row(2)[0].value).strip()
while len(xuhao) != 0:
    # 从模板复制一个文件,并读取这个文件
    fileName = str(sheet.row(initRowNum)[6].value).strip()
    newFilePath = '/Users/luoxiaolei/Desktop/井盖/'+xuhao+fileName+'.xls'
    copyfile('/Users/luoxiaolei/Desktop/井盖/分表.xls', newFilePath)
    # 读取文件必须为xls,否则会报错
    currentXlsx = xlrd.open_workbook(newFilePath,formatting_info=True)
    wb = copy(currentXlsx)
    currentSheet = wb.get_sheet(0)
    # 遍历每一行的每一个单元格
    for collIndex in range(collMinNum,collMaxNum+1):
        # print(str(sheet.row(initRowNum)[collIndex].value).strip())
        # 获取当前单元格的值
        cellValue = str(sheet.row(initRowNum)[collIndex].value).strip()
        # 根据列的索引确定单元格内容
        match collIndex:
            case 0:
                # 序号
                currentSheet.write(2,2,cellValue) # 将单元格内容写入新建的Excel对应单元格
            case 1:
                # 地区
                currentSheet.write(3,2,cellValue)
            case 2:
                # 市县区
                currentSheet.write(3,4,cellValue)
            case 3:
                # 井盖类型
                currentSheet.write(2,4,cellValue)
            case 4:
                # 编号
                currentSheet.write(2,6,cellValue)
            case 5:
                # 公共区域及场所
                currentSheet.write(4,2,cellValue)
            case 6:
                # 位置描述
                currentSheet.write(5,2,cellValue)
            case 7:
                # 坐标
                currentSheet.write(4,4,cellValue)
            case 8:
                # 产权单位
                currentSheet.write(6,2,cellValue)
            case 9:
                # 生产厂家
                currentSheet.write(7,2,cellValue)
            case 10:
                # 井盖生产时间
                currentSheet.write(8,2,cellValue)
            case 11:
                # 井盖材质
                currentSheet.write(9,2,cellValue)
            case 12:
                # 是否为智能井盖
                currentSheet.write(10,2,cellValue)
            case 13:
                # 井盖规格
                currentSheet.write(11,2,cellValue)
            case 14:
                # 井盖承载等级
                currentSheet.write(12,2,cellValue)
            case 15:
                # 病害问题
                currentSheet.write(13,2,cellValue)
            case 16:
                # 风险等级
                currentSheet.write(14,2,cellValue)
            case 17:
                # 调查时间
                currentSheet.write(15,2,cellValue)
            case 18:
                # 计划整改时间
                currentSheet.write(16,2,cellValue)
            case 19:
                # 备注
                currentSheet.write(17,2,cellValue)
            case 20:
                # 填报单位
                currentSheet.write(19,2,cellValue)
            case 21:
                # 填报人
                currentSheet.write(19,4,cellValue)
            case 22:
                # 联系电话
                currentSheet.write(20,2,cellValue)
            case 23:
                # 填报时间
                currentSheet.write(20,4,cellValue)

        wb.save(newFilePath)
        # 写入对应的单元格
    initRowNum = initRowNum + 1
    xuhao =  str(sheet.row(initRowNum)[collMinNum].value).strip()

破茧成蝶:

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-09-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程随想曲 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档