Board logo

标题: Linux下Python3管理Excel表 [打印本页]

作者: admin    时间: 2022-8-6 20:16     标题: Linux下Python3管理Excel表

查看操作系统的信息: [root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core) [root@centos8 ~]# [root@centos8 ~]# uname -r 4.18.0-193.el8.x86_64 [root@centos8 ~]# 查看Python3的版本: [root@centos8 ~]# python3 --version Python 3.6.8 [root@centos8 ~]# Linux连接公网安装第三方库(xlwt): [root@centos8 ~]# pip3 install xlwt==1.3.0 -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting xlwt==1.3.0 Downloading http://mirrors.aliyun.com/pypi/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl (99kB) 100% |████████████████████████████████| 102kB 993kB/s Installing collected packages: xlwt Successfully installed xlwt-1.3.0 [root@centos8 ~]# Linux连接公网安装第三方库(xlrd): [root@centos8 ~]# pip3 install xlrd==1.2.0 -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting xlrd==1.2.0 Downloading http://mirrors.aliyun.com/pypi/packages/b0/16/63576a1a001752e34bf8ea62e367997530dc553b689356b9879339cf45a4/xlrd-1.2.0-py2.py3-none-any.whl (103kB) 100% |████████████████████████████████| 112kB 238kB/s Installing collected packages: xlrd Successfully installed xlrd-1.2.0 [root@centos8 ~]# Linux连接公网安装第三方库(xlutils): [root@centos8 ~]# pip3 install xlutils==2.0.0 -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com Looking in indexes: http://mirrors.aliyun.com/pypi/simple Collecting xlutils==2.0.0 Downloading http://mirrors.aliyun.com/pypi/packages/c7/55/e22ac73dbb316cabb5db28bef6c87044a95914f713a6e81b593f8a0d2f79/xlutils-2.0.0-py2.py3-none-any.whl (55kB) 100% |████████████████████████████████| 61kB 3.0MB/s Requirement already satisfied: xlwt>=0.7.4 in /usr/local/lib/python3.6/site-packages (from xlutils==2.0.0) (1.3.0) Requirement already satisfied: xlrd>=0.7.2 in /usr/local/lib/python3.6/site-packages (from xlutils==2.0.0) (1.2.0) Installing collected packages: xlutils Successfully installed xlutils-2.0.0 [root@centos8 ~]# 列出当前环境所有已经安装的第三方库的名称和其版本号: [root@centos8 ~]# pip3 freeze xlrd==1.2.0 xlutils==2.0.0 xlwt==1.3.0 [root@centos8 ~]# ###### 例子一: 创建一个Excel表的脚本:(xls文件格式) [root@centos8 ~]# cat xx.py #coding=utf-8 import xlwt wbk = xlwt.Workbook() sheet = wbk.add_sheet('苹果') #添加一个工作表,名称为“苹果” sheet.write(0,0,'苹果一周销量情况(公斤)') #往第1行第1列写入内容 sheet.write(1,0,'星期一') #往第2行第1列写入内容 sheet.write(1,1,'星期二') #往第2行第2列写入内容 sheet.write(1,2,'星期三') #往第2行第3列写入内容 sheet.write(1,3,'星期四') sheet.write(1,4,'星期五') sheet.write(1,5,'星期六') sheet.write(1,6,'星期日') A1 = 10 A2 = 20 A3 = 30 A4 = 50 A5 = 40 A6 = 60 A7 = 70 sheet.write(2,0,A1) #往第3行第1列写入内容 sheet.write(2,1,A2) #往第3行第2列写入内容 sheet.write(2,2,A3) #往第3行第3列写入内容 sheet.write(2,3,A4) sheet.write(2,4,A5) sheet.write(2,5,A6) sheet.write(2,6,A7) ### sheet = wbk.add_sheet("梨") #再添加一个工作表,名称为“梨” sheet.write(0,0,'梨一周销量情况(公斤)') #往第1行第1列写入内容 sheet.write(1,0,'星期一') #往第2行第1列写入内容 sheet.write(1,1,'星期二') #往第2行第2列写入内容 sheet.write(1,2,'星期三') #往第2行第3列写入内容 sheet.write(1,3,'星期四') sheet.write(1,4,'星期五') sheet.write(1,5,'星期六') sheet.write(1,6,'星期日') A1 = 10.3 A2 = 20.4 A3 = 30.7 A4 = 50.9 A5 = 40 A6 = 60 A7 = 70 sheet.write(2,0,A1) #往第3行第1列写入内容 sheet.write(2,1,A2) #往第3行第2列写入内容 sheet.write(2,2,A3) sheet.write(2,3,A4) sheet.write(2,4,A5) sheet.write(2,5,A6) sheet.write(2,6,A7) wbk.save('/share/销售登记表_2021.xls') #保存Excel表   运行脚本的效果: [root@centos8 ~]# mkdir -p /share [root@centos8 ~]# [root@centos8 ~]# python3 xx.py [root@centos8 ~]# 新生成的Excel表: [root@centos8 ~]# ls /share/ 销售登记表_2021.xls [root@centos8 ~]# 图片1.png 图片2.png ###### 例子二: 创建一个Excel表的脚本:(xlsx文件格式) [root@centos8 ~]# cat xx.py #coding=utf-8 import xlwt wbk = xlwt.Workbook() sheet = wbk.add_sheet('苹果') #添加一个工作表,名称为“苹果” Key_1 = '苹果一周销量情况(公斤)' sheet.write_merge(0, 0, 0, 6, Key_1) #单元格合并后写入内容 sheet.write(1,0,'星期一') #往第2行第1列写入内容 sheet.write(1,1,'星期二') #往第2行第2列写入内容 sheet.write(1,2,'星期三') #往第2行第3列写入内容 sheet.write(1,3,'星期四') sheet.write(1,4,'星期五') sheet.write(1,5,'星期六') sheet.write(1,6,'星期日') A1 = 10 A2 = 20 A3 = 30 A4 = 50 A5 = 40 A6 = 60 A7 = 70 sheet.write(2,0,A1) #往第3行第1列写入内容 sheet.write(2,1,A2) #往第3行第2列写入内容 sheet.write(2,2,A3) #往第3行第3列写入内容 sheet.write(2,3,A4) sheet.write(2,4,A5) sheet.write(2,5,A6) sheet.write(2,6,A7) ### sheet = wbk.add_sheet("香蕉") #再添加一个工作表,名称为“香蕉” style_1 = xlwt.XFStyle() alignment_1 = xlwt.Alignment() alignment_1.horz = 0x02 alignment_1.vert = 0x01 style_1.alignment = alignment_1 # 设置字体居中 Key_1 = '香蕉一周销量情况(公斤)' sheet.write_merge(0, 0, 0, 6, Key_1, style_1) #单元格合并后写入内容 sheet.write(1,0,'星期一') #往第2行第1列写入内容 sheet.write(1,1,'星期二') #往第2行第2列写入内容 sheet.write(1,2,'星期三') #往第2行第3列写入内容 sheet.write(1,3,'星期四') sheet.write(1,4,'星期五') sheet.write(1,5,'星期六') sheet.write(1,6,'星期日') A1 = 10.3 A2 = 20.4 A3 = 30.7 A4 = 50.9 A5 = 40 A6 = 60 A7 = 70 sheet.write(2,0,A1) #往第3行第1列写入内容 sheet.write(2,1,A2) #往第3行第2列写入内容 sheet.write(2,2,A3) sheet.write(2,3,A4) sheet.write(2,4,A5) sheet.write(2,5,A6) sheet.write(2,6,A7) wbk.save('/share/销售登记表_2022.xlsx') #保存Excel表   运行脚本的效果: [root@centos8 ~]# python3 xx.py [root@centos8 ~]# 新生成的Excel表: [root@centos8 ~]# ls /share/ 销售登记表_2021.xls 销售登记表_2022.xlsx [root@centos8 ~]# 图片3.png 图片4.png ###### 例子三: 读取指定单元格内容的脚本: [root@centos8 ~]# cat xx.py #coding=utf-8 import xlrd file_1 = '/share/销售登记表_2021.xls' #指定Excel表 workbook = xlrd.open_workbook(file_1) My_sheet = workbook.sheet_by_name("梨") #定位到指定名称的工作表 My_rows = My_sheet.row_values(2) # 获取第3行全部内容;从0开始计算的 print(My_rows) # 输出一整行的内容 print(type(My_rows)) print("-" * 10) Key_1 = My_rows[0] # 输出所得行的第1个元素的内容 print(Key_1) print(type(Key_1)) print("-" * 10) Key_2 = My_rows[6] # 输出所得行的第7个元素的内容 print(Key_2) print(type(Key_2)) 运行脚本的效果: [root@centos8 ~]# python3 xx.py [10.3, 20.4, 30.7, 50.9, 40.0, 60.0, 70.0] ---------- 10.3 ---------- 70.0 [root@centos8 ~]# ###### 例子四: 读取指定单元格内容的脚本: [root@centos8 ~]# cat xx.py #coding=utf-8 import xlrd file_1 = '/share/销售登记表_2021.xls' #指定Excel表 workbook = xlrd.open_workbook(file_1) My_sheet = workbook.sheets()[1] #定位到第2页的工作表;从0开始计算的 My_cols = My_sheet.col_values(0) # 获取第1列全部内容;从0开始计算的 print(My_cols) # 输出一整列的内容 print(type(My_cols)) print("-" * 10) Key_1 = My_cols[0] # 输出所得列的第1个元素的内容 print(Key_1) print(type(Key_1)) print("-" * 10) Key_2 = My_cols[2] # 输出所得列的第3个元素的内容 print(Key_2) print(type(Key_2)) 运行脚本的效果: [root@centos8 ~]# python3 xx.py ['梨一周销量情况(公斤)', '星期一', 10.3] ---------- 梨一周销量情况(公斤) ---------- 10.3 [root@centos8 ~]# ###### 例子五: 读取指定单元格内容的脚本: [root@centos8 ~]# cat xx.py #coding=utf-8 import xlrd file_1 = '/share/销售登记表_2022.xlsx' #指定Excel表 workbook = xlrd.open_workbook(file_1) Sheet_1 = "香蕉" My_sheet = workbook.sheet_by_name(Sheet_1) #定位到指定名称的工作表 My_rows = My_sheet.row_values(2) # 获取第3行全部内容;从0开始计算的 print(My_rows) # 输出一整行的内容 print(type(My_rows)) print("-" * 10) Key_1 = My_rows[0] # 输出所得行的第1个元素的内容 print(Key_1) print(type(Key_1)) print("-" * 10) Key_2 = My_rows[5] # 输出所得行的第6个元素的内容 print(Key_2) print(type(Key_2)) 运行脚本的效果: [root@centos8 ~]# python3 xx.py [10.3, 20.4, 30.7, 50.9, 40.0, 60.0, 70.0] ---------- 10.3 ---------- 60.0 [root@centos8 ~]# ###### 例子六: 读取指定单元格内容的脚本: [root@centos8 ~]# cat xx.py #coding=utf-8 import xlrd file_1 = '/share/销售登记表_2022.xlsx' #指定Excel表 workbook = xlrd.open_workbook(file_1) Sheet_1 = 1 My_sheet = workbook.sheets()[Sheet_1] #定位到第2页的工作表 My_cols = My_sheet.col_values(0) # 获取第1列全部内容;从0开始计算的 print(My_cols) # 输出一整列的内容 print(type(My_cols)) print("-" * 10) Key_1 = My_cols[0] # 输出所得列的第1个元素的内容 print(Key_1) print(type(Key_1)) print("-" * 10) Key_2 = My_cols[2] # 输出所得列的第3个元素的内容 print(Key_2) print(type(Key_2)) 运行脚本的效果: [root@centos8 ~]# python3 xx.py ['香蕉一周销量情况(公斤)', '星期一', 10.3] ---------- 香蕉一周销量情况(公斤) ---------- 10.3 [root@centos8 ~]# ###### ###### Linux卸载第三方库(xlwt): [root@centos8 ~]# pip3 uninstall xlwt -y Uninstalling xlwt-1.3.0: Successfully uninstalled xlwt-1.3.0 [root@centos8 ~]# Linux卸载第三方库(xlrd): [root@centos8 ~]# pip3 uninstall xlrd -y Uninstalling xlrd-1.2.0: Successfully uninstalled xlrd-1.2.0 [root@centos8 ~]# Linux卸载第三方库(xlutils): [root@centos8 ~]# pip3 uninstall xlutils -y Uninstalling xlutils-2.0.0: Successfully uninstalled xlutils-2.0.0 [root@centos8 ~]# 相关文章: Windows下Python3管理Excel表

图片附件: 图片1.png (2022-8-6 20:10, 21.64 KB) / 下载次数 56
http://blog.zhuohua.store/attachment.php?aid=20173&k=d1450538341171fc753125ba45b56c5c&t=1714369895&sid=NV91S3



图片附件: 图片2.png (2022-8-6 20:10, 21.87 KB) / 下载次数 69
http://blog.zhuohua.store/attachment.php?aid=20174&k=95189a11088a65cdf4cc06c02d51b3c1&t=1714369895&sid=NV91S3



图片附件: 图片3.png (2022-8-6 20:12, 24.59 KB) / 下载次数 52
http://blog.zhuohua.store/attachment.php?aid=20175&k=dea5a187ceecac089abcfbafd43b2038&t=1714369895&sid=NV91S3



图片附件: 图片4.png (2022-8-6 20:12, 25.42 KB) / 下载次数 61
http://blog.zhuohua.store/attachment.php?aid=20176&k=32c9aa2833be269f607a6d965c1d9934&t=1714369895&sid=NV91S3






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2