1、常規(guī)寫入
# -*- 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表示覆蓋,如果下標(biāo)相同,則覆蓋,不寫,下標(biāo)相同,則拋出異常
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): # 寫入數(shù)據(jù),注意拼接下標(biāo)
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): # 寫入數(shù)據(jù),注意拼接下標(biāo)
for line, line_data in enumerate(row_data):
sheet2.write(index + 1, line, line_data)
workbook.save('savexls.xls')
運(yùn)行后
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')
運(yùn)行截圖
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 # 第一個舊表的行數(shù),下面追加就得在這個后面寫入數(shù)據(jù)
copy_read = copy(f) # 把xlrd對象轉(zhuǎn)為xlwt對象
new_sheet = copy_read.add_sheet('new_sheet') # 添加新表,表名不能重復(fù)
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): # 寫入數(shù)據(jù),注意拼接下標(biāo)
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, '新數(shù)據(jù)1')
exist_sheet.write(old_sheet_rows, 1, '新數(shù)據(jù)2')
exist_sheet.write(old_sheet_rows, 2, '新數(shù)據(jù)3')
copy_read.save('append.xlsx')
運(yùn)行截圖
4、設(shè)置對齊,邊框,列寬
# -*- 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() # 字體基本設(shè)置font.name = u'新宋體'font.colour_index = 32764 # 字體顏色font.height = 160 # 字體大小borders = xlwt.Borders()borders.left = xlwt.Borders.THIN # 添加邊框,細(xì)實(shí)線borders.right = xlwt.Borders.THINborders.top = xlwt.Borders.THINborders.bottom = xlwt.Borders.THINsheet.col(6).width = 12 * 256 # 設(shè)置列寬,一個中文等于兩個英文等于兩個字符,12為字符數(shù),256為衡量單位style = xlwt.XFStyle()style.font = fontstyle.alignment = alignstyle.borders = borderssheet.write(6, 8, 'data', style)book.save('style.xls')
以上就是python向xls寫入數(shù)據(jù)(包括合并,邊框,對齊,列寬)的詳細(xì)內(nèi)容,更多關(guān)于python向xls寫入數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:- python寫入數(shù)據(jù)到csv或xlsx文件的3種方法
- python xlsxwriter模塊的使用
- 用python讀取xlsx文件
- 關(guān)于Python 解決Python3.9 pandas.read_excel(‘xxx.xlsx‘)報錯的問題
- python利用xlsxwriter模塊 操作 Excel
- Python讀取xlsx數(shù)據(jù)生成圖標(biāo)代碼實(shí)例
- python3 使用openpyxl將mysql數(shù)據(jù)寫入xlsx的操作