python处理excel数据之替换数据

起因:
学院整理档案,但是模板excel的时间,导员写成了去年。
目的:
写一个python脚本,把所有的excle中的某个替换掉。

在这里插入图片描述
开始找了个脚本,但是并不成功。。
后来输出excel中的文件,发现这个貌似不简单。
在这里插入图片描述
再百度一下,发现这里应该是一个时间数据,所以换方法。

搞定了,爽啊!!
一下子处理完的感觉!!!

在这里插入图片描述

代码:

import openpyxl
import re
import traceback

changeCells = 0

# replace the special content
"""
file: file path : str
mode: type of the operatoration : int
text: the string need to be replaceed : int or str
replaceText: replacement Text : int or str
"""


def changeData(file, mode, text, replaceText):
    # load the file(*.xlsx)
    wb = openpyxl.load_workbook(file)
    # ! deal with one sheet
    ws = wb.worksheets[0]
    global changeCells
    # get rows and columns of file
    rows = ws.max_row
    cols = ws.max_column
    print(rows)
    print(cols)
    changeFlag = False
    try:
        for row in range(1, rows + 1):
            for col in range(1, cols + 1):
                content = ws.cell(row=row, column=col).value
                #print(content)
                #print(ws.cell(row, col).ctype)
                if (content != None):
                    # mode1: fullmatch replacement
                    if (mode == 1):
                        if (content == text):
                            ws.cell(row=row, column=col+2).value = replaceText
                            changeFlag = True
                            changeCells += 1
                    # mode2: partial replacement
                    elif (mode == 2):
                        if (type(content) == str):
                            ws.cell(row=row, column=col).value = content.replace(
                                text, replaceText, 1)
                            changeFlag = True
                            changeCells += 1
                    # mode3: partialmatch and filling
                    elif (mode == 3):
                        if (type(content) == str):
                            ws.cell(row=row, column=col).value = content.replace(
                                text, text + replaceText, 1)
                            changeFlag = True
                            changeCells += 1
                    else:
                        return 0
        # status_1: modified success
        if (changeFlag):
            wb.save(file)
            return changeCells
        # status_2: no modified
        else:
            return changeCells
    # status_3: exception
    except Exception as e:
        print(traceback.format_exc())


# read the content of file
"""
file: file path : str
"""


# def rdxl(file):
#     # load the file(*.xlsx)
#     wb = openpyxl.load_workbook(file)
#     # ! deal with one sheet
#     ws = wb.worksheets[0]
#     global changeCells
#     # get rows and columns of file
#     rows = ws.max_row
#     cols = ws.max_column
#     changeFlag = False
#     cells = 0
#     for row in range(1, rows + 1):
#         for col in range(1, cols + 1):
#             content = ws.cell(row=row, column=col).value
#             print(content)
#             cells += 1
#     print('cells', cells)


import os
from os import path


# def scaner_file(url):
#     file = os.listdir(url)
#     for f in file:
#         real_url = path.join(url, f)
#         if path.isfile(real_url):
#             print(path.abspath(real_url))
#             # 如果是文件,则以绝度路径的方式输出
#         elif path.isdir(real_url):
#             # 如果是目录,则是地柜调研自定义函数 scaner_file (url)进行多次
#             scaner_file(real_url)
#         else:
#
#             print("其他情况")
#             pass
#         print(real_url)



if __name__ == "__main__":
    url="C:\\Users\LXH\Desktop\档案\ceshi\ZY2"
    file = os.listdir(url)
    for f in file:
        real_url = path.join(url, f)
        res = changeData(real_url, 1, '移交日期:', '2023/3/23')
        print(real_url,':已修改 :', res, ' 处')


    #scaner_file("C:\\Users\LXH\Desktop\档案\ceshi\ZY2")

    #res = changeData('C:\\Users\LXH\Desktop\档案\ceshi\ZY2\ZY2106101-陈新月.xlsx', 1, '移交日期:', '2023/3/23')

    #if (res != None):
    #    print('已修改 ', res, ' 处')
    # else:
    #     print('操作失败:\n'+res)
    #rdxl('C:\\Users\LXH\Desktop\档案\ceshi\ZY2\ZY2106101-陈新月.xlsx')

总结:
因为那个2022/3/23的格式一直搞不定,后面就直接用它前面那个单元格,然后列数加2来解决了,也很简单。。

在这里插入图片描述

开心,很有成就感。

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值