当前位置:主页 > 软件编程 > Python代码 >

python向xls写入数据(包括合并,边框,对齐,列宽)

时间:2022-07-30 10:56:19 | 栏目:Python代码 | 点击:

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')

运行后

 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')

运行截图

3、追加写入

源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')

运行截图

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')

您可能感兴趣的文章:

相关文章