介绍
这篇文章主要介绍python处理excel的案例,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
<强> 强>
<强> 强>
import xlwt import xlrd 得到xlutils.copy import 副本 import pandas  as pd 得到pandas import  DataFrame系列 import 操作系统 os.chdir (& # 39;。/& # 39;) #,从指定文件路径读取excel表格 时间=df pd.read_excel (& # 39; D:/mypaper/数据/data.xlsx& # 39;) #,查看df内容
#,根据年龄算出出生年份,增加一列 import datetime import 操作系统 时间=year datetime.datetime.now()岁#获取当前系统时间对应的年份 df(& # 39;出生# 39;],=,year-df[& # 39;年龄# 39;】 df.to_excel (& # 39; result.xlsx& # 39;) #保存到当前工作目录,可以用os.getcwd()查看 #查看下此时df的内容,可以看到已经生成了出生这一列
<强> 强>
#,定义方法:读取指定目录下excel文件某个板单元格的值 def excel_read (file_path表,x, y): ,,,,data =, xlrd.open_workbook (file_path) ,,,,table =, data.sheet_by_name(表) ,,,,return table.cell (y, x) value , #,定义方法:单元格值及样式 时间=write_obj_list [] def concat_obj(关口、行值): ,,,write_obj_list.append({& # 39;关口# 39;:关口,& # 39;行# 39;:行,& # 39;价值# 39;:价值\ & # 39;风格# 39;:xlwt.easyxf(& # 39;字体:name 宋体,height 280;对齐:,horiz 中心# 39;)}) , #,定义方法:合并单元格 def merge_unit (srows erows scols,生态,值): ,,,write_obj_list.append ({& # 39; id # 39;: & # 39;合并# 39;,& # 39;srows& # 39;: srows, & # 39; erows& # 39;: erows, & # 39; scols& # 39;: scols, \ & # 39;ecols& # 39;:生态,& # 39;价值# 39;:价值,& # 39;风格# 39;:xlwt.easyxf(& # 39;字体:name 宋体,height 280;对齐:,horiz 中心# 39;)}) , #,定义方法:更新excel excel_update (file_path write_obj_list new_path): ,,,old_excel =, xlrd.open_workbook (file_path, formatting_info=True) ,,,#管道作用 ,,,new_excel =,副本(old_excel) ,,,& # 39;& # 39;& # 39; ,,,通过get_sheet()获取的表有写()方法 ,,,& # 39;& # 39;& # 39; ,,,sheet1 =, new_excel.get_sheet (0) ,,,& # 39;& # 39;& # 39; ,,,1代表是修改第几个工作表里,从0开始算是第一个。此处修改第一个工作表 ,,,& # 39;& # 39;& # 39; ,,,for item 拷贝write_obj_list: ,,,,,,,if & # 39; id # 39;, not 拷贝item.keys (): ,,,,,,,,,,,if & # 39;风格# 39;,拷贝item.keys (): ,,,,,,,,,,,,,,,sheet1.write(项目[& # 39;行# 39;],,项目[& # 39;关口# 39;],,项目[& # 39;价值# 39;],项[& # 39;风格# 39;]) ,,,,,,,,,,,其他的: ,,,,,,,,,,,,,,,sheet1.write(项目[& # 39;行# 39;],,项目[& # 39;关口# 39;],,项目[& # 39;价值# 39;]) ,,,,,,,其他的: ,,,,,,,,,,,if & # 39;风格# 39;,拷贝item.keys (): ,,,,,,,,,,,,,,,sheet1.write_merge(项目[& # 39;srows& # 39;],项[& # 39;erows& # 39;],项[& # 39;scols& # 39;],,项目[& # 39;ecols& # 39;],,项目[& # 39;价值# 39;],项[& # 39;风格# 39;]) ,,,,,,,,,,,其他的: ,,,,,,,,,,,,,,,sheet1.write_merge(项目[& # 39;srows& # 39;],项[& # 39;erows& # 39;],项[& # 39;scols& # 39;],,项目[& # 39;ecols& # 39;],,项目[& # 39;价值# 39;]) ,,,& # 39;& # 39;& # 39; ,,,如果报错,dict_items has no attributes 排序 ,,,把syle源码中——alist.sort(),修改为——祝辞,排序(倾斜的) ,,,一共修改2次 ,,,& # 39;& # 39;& # 39; ,,,new_excel.save (file_path) , #参数详解 #,srows:合并的起始行数 #,erows:合并的结束行数 #,scols:合并的起始列数 #,生态:合并的结束列数 #,价值:合并单元格后的填充的值 #,风格:合并后填充风格: #,,,,,字体:,name 宋体 #,,,,,height 280; #,,,,,对齐:horiz 中心 nullpython处理excel的案例