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にデータを書き込む資料は他の関連記事に注目してください。