Python xlsxwriter

发布时间 2023-04-22 21:39:51作者: NoneButNow
# pip install xlsxwriter
import xlsxwriter
import datetime
# 创建excel文件
excel_ = xlsxwriter.Workbook('test_excel.xlsx')
# 创建sheet表单
sheet_ = excel_.add_worksheet('test_sheet')
# 写
sheet_.write('A1', 'Kate')  # 字符串形式指明行列坐标(字母列数字行),第1行第1列
sheet_.write('A2', 'Tom')
sheet_.write(0, 1, 17)  # 数字形式指明行列坐标(前行后列),第1行第2列,下标都从0开始
sheet_.write(1, 1, 22)
sheet_.write(2, 1, '=sum(B1:B2)')  # 写入公式
sheet_.insert_image(3, 3, '5982adf6c88ea_610.jpg') # 写入图片
sheet_.insert_image(3, 3, '5982adf6c88ea_610.jpg', {'url': 'https://www.baidu.com/'}) # 赋予图片超链接
date_ = excel_.add_format({'num_format': 'yyyy-mm-dd'}) # 设置日期格式
sheet_.write(3, 1, datetime.datetime.strptime('2000-9-9', '%Y-%m-%d'), date_) # 写入日期
# 关闭excel文件
excel_.close()