Python简单读写Xls格式文档的方法示例
作者:sulin 发布时间:2021-11-02 13:27:30
标签:Python,Xls格式文档
本文实例讲述了Python简单读写Xls格式文档的方法。分享给大家供大家参考,具体如下:
1. 模块安装
使用pip install
命令安装,
即:
pip install xlrd
pip install xlwt
如下图:
2. python 代码
import xlrd
import xlwt
import datetime
def set_style(name,height,format,bold=False):
style = xlwt.XFStyle()
if format.strip()!='':
style.num_format_str =format
font = xlwt.Font()
font.name=name
font.bold=bold
font.color_index=4
font.height=height
alignment = xlwt.Alignment()
#HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
alignment.horz = xlwt.Alignment.HORZ_CENTER
#VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
alignment.vert = xlwt.Alignment.VERT_CENTER
style.alignment = alignment
style.font=font
return style
def set_colstyle(sheet,cindex):
col=sheet.col(cindex)
col.width =256*20
#col.height =100
def writeXls(path):
wb = xlwt.Workbook()
sheet = wb.add_sheet('测试',cell_overwrite_ok=True)
set_colstyle(sheet,3)
#标题
heads=['姓名','学科','分数','日期']
for h in range(0,len(heads)):
sheet.write(0,h,heads[h],set_style('Arial',300,'',True))
#数据
sheet.write_merge(1,2,0,0,'张三',set_style('Arial',300,'',False))
sheet.write(1,1,'语文',set_style('Arial',240,'',False))
sheet.write(1,2,85,set_style('Arial',240,'',False))
sheet.write(1,3,datetime.date.today(),set_style('Arial',240,'yyyy/mm/dd',False))
sheet.write(2,1,'数学',set_style('Arial',240,'',False))
sheet.write(2,2,85,set_style('Arial',240,'',False))
sheet.write(2,3,datetime.date.today(),set_style('Arial',240,'yyyy/mm/dd',False))
sheet.write_merge(3,4,0,0,'李四',set_style('Arial',300,'',False))
sheet.write(3,1,'语文',set_style('Arial',240,'',False))
sheet.write(3,2,95,set_style('Arial',240,'',False))
sheet.write(3,3,datetime.date.today(),set_style('Arial',240,'yyyy/mm/dd',False))
sheet.write(4,1,'数学',set_style('Arial',240,'',False))
sheet.write(4,2,95,set_style('Arial',240,'',False))
sheet.write(4,3,datetime.date.today(),set_style('Arial',240,'yyyy/mm/dd',False))
wb.save(path)
def ismerge(sheet,merge,r,c):
#merge=sheet.merged_cells
for m in merge:
if r>=m[0] and r<m[1] and c==m[2]:
r=m[0]
c==m[2]
break
return r,c
def readXls(path):
wb=xlrd.open_workbook(path,formatting_info=True)
#sheetname=wb.sheet_names()[0]
sheet=wb.sheet_by_index(0)
rows=sheet.nrows
cols=sheet.ncols
merge=sheet.merged_cells
#merged_cells返回的这四个参数的含义是:(row,row_range,col,col_range),
#其中[row,row_range)包括row,不包括row_range
print('--------------------------------------------------------------------')
for r in range(0,rows):
listStr = []
for c in range(0,cols):
merg=ismerge(sheet,merge,r,c)
if (sheet.cell(merg[0],merg[1]).ctype==3):
data_value=xlrd.xldate_as_tuple(sheet.cell_value(merg[0],merg[1]),wb.datemode)
#print(datetime.date(*data_value[:3]).strftime('%Y/%m/%d'))
listStr.append(datetime.date(*data_value[:3]).strftime('%Y/%m/%d'))
else:
#print(sheet.cell_value(merg[0],merg[1]))
listStr.append(sheet.cell_value(merg[0],merg[1]))
#print(sheet.cell(merg[0],merg[1]).value)
#print(sheet.cell(r,c).ctype)
print(' |\t'.join(str(s) for s in listStr if s not in [None]))
print('--------------------------------------------------------------------')
if __name__ == '__main__':
#writeXls('H:\测试.xls')
readXls('H:\测试.xls')
3.效果展示
希望本文所述对大家Python程序设计有所帮助。
来源:https://www.cnblogs.com/linsu/p/8960766.html


猜你喜欢
- 本文介绍MySQL与Redis缓存的同步的两种方案方案1:通过MySQL自动同步刷新Redis,MySQL触发器+UDF函数实现方案2:解析
- 协程的特点1.该任务的业务代码主动要求切换,即主动让出执行权限2.发生了IO,导致执行阻塞(使用channel让协程阻塞)与线程本质的不同C
- 使用步骤:1、安装:npm i jquery-contextmenu --save-dev2、在main.js文件中引包 im
- 前言一些公司内部的CMS系统存在某些内容让指定的用户有权限访问,这时候可以用django自带的权限管理进行限制,比较方便。缺点:django
- 简介Pycharm安装以后必须激活后,才能正常的使用。否则就不能使用。激活PyCharm1、Activation code激活优点:Wind
- 在Python中的while或者for循环之后还可以有else子句,作用是for循环中if条件一直不满足,则最后就执行else语句。for
- 每次安装总是有些不同,这次用这种方式尝试一下,也记录一下。1、首先需要去下载rpm包:镜像地址:http://mysql.mirrors.p
- 1.将经常要用到的字段(比如经常要用这些字段来排序,或者用来做搜索),则最好将这些字段设为索引。2.字段的种类尽可能用int 或者tinyi
- PDO::setAttributePDO::setAttribute — 设置属性(PHP 5 >= 5.1.0, PECL pdo
- 我们今天就来看一下PHP 7正式版的算法和 wordpress 应用在其上的性能表现。PHP7 的安装,真是非常地向下兼容,下载,解压,把之
- 本文主要介绍了Python中list[::-1]的几种用法,分享给大家,具体如下:s = "abcde"list的[]中
- 什么是分页技术 分页,是一种将所有数据分段展示给用户的技术.用户每次看到的不是全部数据,而是其中的一部分,如果在其中没有找到自习自
- 今天一同事需要整理http://ics.cnvd.org.cn/工控漏洞库里面的信息,一看960多个要整理到什么时候才结束。所以我决定写个爬
- 本文实例讲述了PHP日志LOG类定义与用法。分享给大家供大家参考,具体如下:<?php/*** PHP log 类 */class C
- 简介在各个语言之中都有时间类型的处理,因为这个地球是圆的(我仿佛在讲废话),有多个时区,每个时区的时间不一样,在程序中有必要存在一种方式,或
- 概述迁移学习 (Transfer Learning) 是把已学训练好的模型参数用作新训练模型的起始参数. 迁移学习是深度学习中非常重要和常用
- 作者的blog:http://www.planabc.netz-index属性简介引用:z-index : auto | numberaut
- python自带的IDLE使用起来非常方便,尤其是在编写调试小段代码的时候,但是安装了Anaconda的同志可能会发现,无法像直接安装pyt
- NumPy's main object is the homogeneous multidimensional array. It
- 个人类型和海外类型的小程序不支持 web-view 标签 也就是说个人申请的小程序,就别想跳转了!!!!1.开发的时候,我们难免