Python3 removes Excel blanks
[environment]
Windows 10, Python 3.6, using third-party package openpyxl.
[config.ini]
[config]; Excel file name XlFile=D:\ test\ test.xlsx; form name SheetName=Sheet1 to be processed
[trim_cell_for_excel.py]
# encoding: utf-8# author: walker# date: 2018-09-2 blank summary: blank 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 "read configuration file"global XlFile" before and after removing the string in the Excel cell SheetName cfg = ConfigParser () cfgFile = os.path.join (cur_dir_fullpath, ringing 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'): # remove the utf8 bom header 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 succeeded') def Main (): print ('Load% s...'% XlFile) wb = openpyxl.load_workbook (XlFile) print ('Load% s successors'% 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 clients'% XlFile) if _ _ name__ = ='_ main__': ReadConfig () Main () print ('Time total:% .2fs'% (time.time ()-StartTime)) print ('Current time:% s'% time.strftime ( '% Y-%m-%d% HRV% MRV% S' Time.localtime (time.time ()
[related reading]
Working with Excel Files in Python
* walker * *