Python3 去除 Excel 空白
发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,【环境】Windows 10 下,Python 3.6,使用第三方包 openpyxl。【config.ini】[config]; Excel 文件名XlFile=D:\test\test.xlsx;
千家信息网最后更新 2025年11月08日Python3 去除 Excel 空白
【环境】
Windows 10 下,Python 3.6,使用第三方包 openpyxl。
【config.ini】
[config]; Excel 文件名XlFile=D:\test\test.xlsx; 需处理的表单名SheetName=Sheet1
【trim_cell_for_excel.py】
# encoding: utf-8# author: walker# date: 2018-09-26# summary: 去除 Excel 单元格内字符串前后的空白import osimport sysimport timeimport openpyxlfrom configparser import ConfigParserStartTime = time.time()cur_dir_fullpath = os.path.dirname(os.path.abspath(__file__))XlFile = r''SheetName = r''def ReadConfig(): r""" 读取配置文件 """ global XlFile, SheetName cfg = ConfigParser() cfgFile = os.path.join(cur_dir_fullpath, r'config.ini') if not os.path.exists(cfgFile): input(cfgFile + ' not found') sys.exit(-1) with open(cfgFile, mode='rb') as f: content = f.read() if content.startswith(b'\xef\xbb\xbf'): # 去掉 utf8 bom 头 content = content[3:] cfg.read_string(content.decode('utf8')) if not cfg.sections(): input('Read config.ini failed...') sys.exit(-1) XlFile = cfg.get('config', 'XlFile').strip() if not os.path.exists(XlFile): print('Error: not exists %s' % XlFile) sys.exit(-1) print('XlFile: %s' % XlFile) SheetName = cfg.get('config', 'SheetName').strip() print('SheetName: %s' % SheetName) print('Read config.ini successed!')def Main(): print('Load %s ...' % XlFile) wb = openpyxl.load_workbook(XlFile) print('Load %s success!' % XlFile) sheet = wb[SheetName] for i in range(1, sheet.max_row + 1): for j in range(1, sheet.max_column + 1): rawVal = sheet.cell(i, j).value if not isinstance(rawVal, str): continue sheet.cell(i, j).value = rawVal.strip() print('Save %s ...' % XlFile) wb.save(XlFile) print('Save %s success!' % XlFile)if __name__ == '__main__': ReadConfig() Main() print('Time total: %.2fs' % (time.time() - StartTime)) print('Current time: %s' % time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(time.time())))【相关阅读】
Working with Excel Files in Python
*** walker ***
文件
空白
单元
字符
字符串
文件名
环境
第三方
表单
格内
处理
配置
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
北京速能数码网络技术
网络安全和信息化专家库名单
服务器隔音降温箱
选择数据库的4 C原则是内
网络技术面试问答
医院网络安全演练小结
ix000数据库服务器
电脑搭建web服务器
网络安全等级保护网站上级单位
网络技术专升本学什么专业
小学生家长网络安全演讲稿
公司访客网络安全
上海盛业网络技术有限公司
东莞pc软件开发热线
内蒙古自动化软件开发风格
服务器拷片
电脑许可管理服务器
vb sql 数据库查询
南通软件开发培训班教学视频
网络安全宣传图片大全小学
务川索菲亚大酒店网络安全
安徽综合软件开发定制价格
数据库查询使用心得
电商领域里的网络安全事件
计算机常用的数据库
前端还要会数据库吗
精选服务器机箱推荐
私有云平台上建数据库
华为服务器所有网卡一直闪烁
电商平台与网络安全