利用python怎么向xls写入数据

发布时间:2021-02-04 16:08:08 作者:Leah
来源:亿速云 阅读:776

本篇文章给大家分享的是有关利用python怎么向xls写入数据,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

1、常规写入

# -*- encoding=utf-8 -*-
import xlwt

if __name__ == '__main__':
  head = ['姓名', '年龄', '出生年月']
  data = [
    ['盖伦', '20', '2012-02-04'],
    ['赵信', '18', '2013-05-12'],
    ['女枪', '18', '2015-12-12'],
    ['剑圣', '20', '2012-11-14'],
    ]
  workbook = xlwt.Workbook()
  # 添加一个表,
  # cell_overwrite_ok=True表示覆盖,如果下标相同,则覆盖,不写,下标相同,则抛出异常
  sheet1 = workbook.add_sheet('Sheet1', cell_overwrite_ok=False)
  for index, info in enumerate(head): # 写入表头
    sheet1.write(0, index, info)
  for index, row_data in enumerate(data): # 写入数据,注意拼接下标
    for line, line_data in enumerate(row_data):
      sheet1.write(index + 1, line, line_data)

  sheet2 = workbook.add_sheet('Sheet2') # 添加一个表
  for index, info in enumerate(head): # 写入表头
    sheet2.write(0, index, info)
  for index, row_data in enumerate(data): # 写入数据,注意拼接下标
    for line, line_data in enumerate(row_data):
      sheet2.write(index + 1, line, line_data)
  workbook.save('savexls.xls')

运行后

利用python怎么向xls写入数据

 2、合并单元格写入

# -*- encoding=utf-8 -*-
import xlwt

if __name__ == '__main__':
  workbook = xlwt.Workbook()
  sheet1 = workbook.add_sheet('Sheet1')
  # 合并从0行到0行,从0列到1列
  sheet1.write_merge(0, 0, 0, 1, '合并单元格')

  # 合并从2行到4行,从0列到3列
  sheet1.write_merge(2, 4, 0, 3, '合并单元格')
  workbook.save('merge.xls')

运行截图

利用python怎么向xls写入数据

3、追加写入

源xls文件

利用python怎么向xls写入数据

# -*- encoding=utf-8 -*-

import xlrd
from xlutils.copy import copy

if __name__ == '__main__':
  pass
  filename = 'readxls.xls'
  f = xlrd.open_workbook(filename) # 打开Excel为xlrd对象
  old_sheet = f.sheet_by_index(0) # 取到第一个旧表
  old_sheet_rows = old_sheet.nrows # 第一个旧表的行数,下面追加就得在这个后面写入数据
  copy_read = copy(f) # 把xlrd对象转为xlwt对象
  new_sheet = copy_read.add_sheet('new_sheet') # 添加新表,表名不能重复
  head = ['name', 'age', 'birthday']
  data = [[1, 2, 3], [4, '2019/02/01', 6], [7, 8, 9]]
  for index, info in enumerate(head): # 写入表头
    new_sheet.write(0, index, info)
  for index, row_data in enumerate(data): # 写入数据,注意拼接下标
    for line, line_data in enumerate(row_data):
      new_sheet.write(index + 1, line, line_data)
  exist_sheet = copy_read.get_sheet(0) # 取旧表
  exist_sheet.write(old_sheet_rows, 0, '新数据1')
  exist_sheet.write(old_sheet_rows, 1, '新数据2')
  exist_sheet.write(old_sheet_rows, 2, '新数据3')
  copy_read.save('append.xlsx')

运行截图

利用python怎么向xls写入数据

4、设置对齐,边框,列宽

# -*- encoding=utf-8 -*-import xlwtbook = xlwt.Workbook()sheet = book.add_sheet('sheet')sheet.write(6, 6, 'data')align = xlwt.Alignment()align.horz = xlwt.Alignment.HORZ_CENTER # 水平居中align.vert = xlwt.Alignment.VERT_CENTER # 垂直居中font = xlwt.Font() # 字体基本设置font.name = u'新宋体'font.colour_index = 32764 # 字体颜色font.height = 160 # 字体大小borders = xlwt.Borders()borders.left = xlwt.Borders.THIN # 添加边框,细实线borders.right = xlwt.Borders.THINborders.top = xlwt.Borders.THINborders.bottom = xlwt.Borders.THINsheet.col(6).width = 12 * 256 # 设置列宽,一个中文等于两个英文等于两个字符,12为字符数,256为衡量单位style = xlwt.XFStyle()style.font = fontstyle.alignment = alignstyle.borders = borderssheet.write(6, 8, 'data', style)book.save('style.xls')

利用python怎么向xls写入数据

以上就是利用python怎么向xls写入数据,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. C# 向共享文件中写入数据
  2. Python向excel中写入数据的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python xls

上一篇:如何在asp.net core的中间件中返回具体的页面

下一篇:React中共享组件逻辑的方式有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》