美文网首页
python 3 读写excel表格

python 3 读写excel表格

作者: 令狐小冲 | 来源:发表于2022-04-19 10:59 被阅读0次

前些天有个程序员就是利用python 技术,给医护人员把疫情使用的数据导到表格里,方便使用。用了一个小时。其实不是很麻烦,这些东西网上有各种示例。只需要改一改数据源,表格的模板。把数据填充进去即可。

下面就说说用python 这个语言来简单的读取表格数据。比其他语言简单的多了。但还是要有语言的基础,这里的语言不是说汉语、英语,广东话。是编程语言。

python读取Excel表格文件,例如获取这个文件的数据

python读取Excel表格文件,需要如下步骤:

1、安装Excel读取数据的库-----xlrd

      直接pip install xlrd安装xlrd库

     #引入Excel库的 xlrd

     import xlrd

2、获取Excel文件的位置并且读取进来

      入需要读取Excel表格的路径

      data = xlrd.open_workbook('F:\python\test.xls')

      table =data.sheets()[0]

3、读取指定的行和列的内容,并将内容存储在列表中(将第三列的时间格式转换)

#创建一个空列表,存储Excel的数据

tables =[]

#将excel表格内容导入到tables列表中

def import_excel(excel):

  for rown inrange(excel.nrows):

   array ={'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''}

   array['road_name'] =table.cell_value(rown,0)

   array['bus_plate'] =table.cell_value(rown,1)

   #将Excel表格中的时间格式转化

   if table.cell(rown,2).ctype ==3:

     date =xldate_as_tuple(table.cell(rown,2).value,0)

     array['timeline'] =datetime.datetime(*date)

   array['road_type'] =table.cell_value(rown,3)

   array['site'] =table.cell_value(rown,4)

   tables.append(array)

4、运行程序

if__name__ =='__main__':

  #将excel表格的内容导入到列表中

  import_excel(table)

  #验证Excel文件存储到列表中的数据

  fori intables:

    print(i)

6、完整的程序代码:

import xlrd

from xlrd import xldate_as_tuple

import datetime

#导入需要读取的第一个Excel表格的路径

data1 = xlrd.open_workbook('F:\python\test.xls')

table = data1.sheets()[0]

#创建一个空列表,存储Excel的数据

tables = []

#将excel表格内容导入到tables列表中

def import_excel(excel):

  for rown in range(excel.nrows):

  array = {'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''}

  array['road_name'] = table.cell_value(rown,0)

  array['bus_plate'] = table.cell_value(rown,1)

  if table.cell(rown,2).ctype == 3:

    date = xldate_as_tuple(table.cell(rown,2).value,0)

    array['timeline'] = datetime.datetime(*date)

  array['road_type'] = table.cell_value(rown,3)

  array['site'] = table.cell_value(rown,4)

  tables.append(array)

if __name__ == '__main__':

  #将excel表格的内容导入到列表中

  import_excel(table)

  for i in tables:

    print(i)

上边的代码是说怎么把数据从表格文件里读出来,再说说再么写excel文件 。

费话不多说,上代码。

#coding=utf-8

importxlwt

#设置表格样式

defset_stlye(name,height,bold=False):

 #初始化样式

 style =xlwt.XFStyle()

 #创建字体

 font =xlwt.Font()

 font.bold =bold

 font.colour_index =4

 font.height =height

 font.name =name

 style.font =font

 returnstyle

#写入数据

defwrite_excel():

 f =xlwt.Workbook()

 #创建sheet1

 sheet1 =f.add_sheet(u'sheet1',cell_overwrite_ok=True)

 row0 =[u'业务',u'状态',u'北京',u'上海',u'广州',u'深圳',u'状态小计',u'合计']

 column0 =[u'机票',u'船票',u'火车票',u'汽车票',u'其他']

 status =[u'预定',u'出票',u'退票',u'业务小计']

 fori inrange(0,len(row0)):

  sheet1.write(0,i,row0[i],set_stlye("Time New Roman",220,True))

 i,j =1,0

 whilei <4*len(column0): #控制循环:每次加4

  #第一列

  sheet1.write_merge(i,i+3,0,0,column0[j],set_stlye('Arial',220,True))

  #最后一列

  sheet1.write_merge(i,i+3,7,7)

  i +=4

 sheet1.write_merge(21,21,0,1,u'合计',set_stlye("Time New Roman",220,True))

 i=0

 whilei<4*len(column0): #控制外层循环:每次加4

  forj inrange(0,len(status)): #控制内层循环:设置每一行内容

   sheet1.write(i+j+1,1,status[j])

  i +=4

 #创建sheet2

 sheet2 =f.add_sheet(u'sheet2',cell_overwrite_ok=True)

 row0 =[u'姓名',u'年龄',u'出生日期',u'爱好',u'关系']

 column0 =[u'UZI',u'Faker',u'大司马',u'PDD',u'冯提莫']

 #生成第一行

 fori inrange(0,len(row0)):

  sheet2.write(0,i,row0[i],set_stlye('Times New Roman',220,True))

 #生成第一列

 fori inrange(0,len(column0)):

  sheet2.write(i+1,0,column0[i],set_stlye('Times New Roman',220,True))

 f.save('data.xls')

if__name__ =='__main__':

 write_excel()~

简单的多的多,有人说程序员脱离的网络就不会写代码了,这话说的没毛病。要站在巨人的肩膀上前进才行。只有你知道的多,你才能做的更多。

相关文章

网友评论

      本文标题:python 3 读写excel表格

      本文链接:https://www.haomeiwen.com/subject/bjrsertx.html