有python操作excel文件的需求,需要把每天资料按月导入一个excel表,每天1列。 查了一下网上资料,没想到那么不方便。不知道是不是找到的资料旧了。
先试一下。
这个要使用xlwt模块。我最先查到的也是这个。后面才发现这个模块只能新建,不能修改。修改得用另外一个模块。 下载安装
Collecting xlwt Downloading https://files.pythonhosted.org/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl (99kB) 100% |████████████████████████████████| 102kB 86kB/s Installing collected packages: xlwt Successfully installed xlwt-1.3.0 You are using pip version 9.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.操作方法网上一堆,不详述了。
excelfilename=‘aaa.xls' f = xlwt.Workbook() sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet for j in range(31): #列名,31天 sheet1.write(0,j,j+1) i=1 for data in datas: #datas就是每天的数据 sheet1.write(i,day-1,data) #写入当天日期对应的列 i = i + 1 f.save(excelfilename) #保存文件注意:xlwt操作excel的行和列是从0开始的。
等我打算写入第二天数据时候,才发现xlwt模块不行,它不能对现有的excel文件进行修改操作,只能覆盖。 好吧,继续查找,发现得用openpyxl模块才行。openpyxl模块可实现对excel文件的读、写和修改。 下载安装
Collecting OpenPyXL Downloading https://files.pythonhosted.org/packages/d6/26/eb28e975b7a37aad38d7ec4f7a0f652bdee6ecf36e6bd06f473c5af9b87b/openpyxl-2.6.4.tar.gz (173kB) 100% |████████████████████████████████| 174kB 12kB/s Collecting jdcal (from OpenPyXL) Downloading https://files.pythonhosted.org/packages/f0/da/572cbc0bc582390480bbd7c4e93d14dc46079778ed915b505dc494b37c57/jdcal-1.4.1-py2.py3-none-any.whl Collecting et_xmlfile (from OpenPyXL) Downloading https://files.pythonhosted.org/packages/22/28/a99c42aea746e18382ad9fb36f64c1c1f04216f41797f2f0fa567da11388/et_xmlfile-1.0.1.tar.gz Installing collected packages: jdcal, et-xmlfile, OpenPyXL Running setup.py install for et-xmlfile ... done Running setup.py install for OpenPyXL ... done Successfully installed OpenPyXL-2.6.4 et-xmlfile-1.0.1 jdcal-1.4.1 You are using pip version 9.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.结果使用时候又遇到问题了。 刚才我用xlwt保存的文件是aaa.xls,openpyxl只能处理xlsx文件,不能处理xls文件。而xlwt模块只能写xls文件,不能写xlsx文件(写成aaa.xlsx程序不会报错,但文件无法直接打开,会报错)。。 没办法,只好再去找转换的办法(xls to xlsx),这个后面说。 转换后就可以操作了。操作方法网上一堆,不详述了。
file_path=‘aaa.xlsx' f = load_workbook(file_path) sheet1 =f['Sheet1'] i=1 for data in datas: sheet1.cell(i+1,day).value=data i = i + 1 f.save(file_path) #保存文件注意:openpyxl操作excel的行和列是从1开始的。这个和xlwt不一样。
刚才说了,需要把xls文件转xlsx,网上又查了一下,可以用pandas模块。 下载安装
Downloading https://files.pythonhosted.org/packages/61/57/6c233cc63597c6aa6337e717bdeabf791e8b618e9c893922a223e4e41cf4/pandas-0.24.2-cp27-cp27m-win_amd64.whl (8.3MB) 100% |████████████████████████████████| 8.3MB 16kB/s Requirement already satisfied: numpy>=1.12.0 in c:\python27\lib\site-packages (from pandas) Requirement already satisfied: pytz>=2011k in c:\python27\lib\site-packages (from pandas) Requirement already satisfied: python-dateutil>=2.5.0 in c:\python27\lib\site-packages (from pandas) Requirement already satisfied: six>=1.5 in c:\python27\lib\site-packages (from python-dateutil>=2.5.0->pandas) Installing collected packages: pandas Successfully installed pandas-0.24.2 You are using pip version 9.0.3, however version 19.3.1 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.使用
#把xls转化为xlsx文件 x = pd.read_excel(‘aaa.xls',index=False,header=0) x.to_excel('aaa.xlsx',index=False)注意:我开始时候没有按照上面代码中那样写参数,结果行和列里莫名其妙多了很多Unnamed:0 查了网上才知道,出现这种情况是因为存aaa.xls的时候存了索引,而读的时候认为没有索引,所以就给内容自动加了行和列Unname:0。 那么如何解决这个问题呢?其实很简单,要么存的时候不存索引,要么读的时候需要认为里面有索引。
第一种解决方式:不存索引
x.to_csv(path,index=False,header=False)
第二种解决方式:声明文件第一列为索引,第一行为列名(默认就是)
x = pd.read_csv(path,index_col=0,header=0) 我有列名,没有行号,所以index=False,header=0。
又,pandas模块运行是依靠xlwt和openpyxl模块的。不安装这个2个模块,无法适用。
看到网上还介绍了一个模块xlrd,既可读取xls文件也可读取xlsx文件。 因为只能读,所以也就没有使用了。
