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

運行後

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

以上就是python向xls寫入數據(包括合並,邊框,對齊,列寬)的詳細內容,更多關於python向xls寫入數據的資料請關註WalkonNet其它相關文章!