python学习01–批量将excel表名和首行字段名改为小写

python学习01–批量将excel表名和首行字段名改为小写

在操作postgresql数据库时,普通的关系表可以通过Navicat录入postgresql数据库中,但是postgresql数据库对大小写敏感,为了工程方便,所有表名和字段名统一为小写,为了入库方便,利用python修改excel文件名,以及首行代表的字段名

# -*- coding: UTF-8 -*-
import os
import openpyxl


# 获取当前目录中的所有文件
def file_name(file_dir):
    File_Name = []
    for files in os.listdir(file_dir):
        if os.path.splitext(files)[1] == '.xlsx':
            File_Name.append(files)
    return File_Name


# 处理excel
def dealExcel(path):
    data = openpyxl.load_workbook(path) # 可读可写
#遍历所有的sheet

    for sheet in data:
# 把第一行的值全部换成小写字母
#获取列数
        sheet.title=str(sheet.title).lower()
        columCount=sheet.max_column
        rowCount=sheet.max_row
        if(rowCount==0):
           continue
        if(columCount==0):
            continue
        else:
            for i in range(0,columCount):
                b3 = sheet.cell(1,i+1)
                valueModify=str(b3.value).lower()
                b3.value=str(b3.value).lower()

    data.close()
    os.remove(path)
    data.save(path.lower())
currentDir = os.getcwd()
txt_file_name = file_name(currentDir)
for filePath in txt_file_name:
    path = currentDir + "/" + filePath
    print("正在处理: ",format(path))
    dealExcel(path)

print("处理成功!")


这里处理的是同级目录下的所有*.xlsx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值