其他分享
首页 > 其他分享> > xlsxwriter新建Excel文件

xlsxwriter新建Excel文件

作者:互联网

使用方法

安装请参考PyPI官网:https://pypi.org/

新建Excel

import xlsxwriter
#创建Excel 名称为demo.xlsx
workbook = xlsxwriter.Workbook("demo.xlsx")

workbook.close()

创建Sheet

#创建Sheet 名称为demo
worksheet = workbook.add_worksheet("demo")

设置宽度

#第0行第0列宽度为40
worksheet.set_column(0,0,40)

#字体添加样式

#加粗 红色字体 字体大小20
format_dic = {
    "bold":True,
    "font_color":"#DC143C",
    "font_size":20
}
text_format = workbook.add_format(format_dic)

写入

第一列写入姓名

#第0行第0列内容为【Name】样式为 加粗,红色,大小20
worksheet.write(0,0,"姓名",text_format)
worksheet.write(1,0,"张三")
worksheet.write(2,0,"李四")

第二列写入日期,日期需要添加字符样式

#设置字符样式
format_dic['num_format'] = "yyyy-mm-dd HH:MM:SS"
date_format = workbook.add_format(format_dic)
#第二列表头
worksheet.write(0,1,"时间",text_format)
#第2行第2列内容
date_time = datetime.datetime.strptime("2013-01-03","%Y-%m-%d")
worksheet.write_datetime(1,1,date_time,date_format)
#第3行第2列内容
worksheet.write_datetime(2,1,datetime.datetime.now(),date_format)

具体实现代码

import xlsxwriter
import datetime

workbook = xlsxwriter.Workbook("demo.xlsx")
#创建Sheet 名称为demo
worksheet = workbook.add_worksheet("demo")
#第0行第0列宽度为40
worksheet.set_column(0,0,40)
#字体添加样式
format_dic = {
    "bold":True,
    "font_color":"#DC143C",
    "font_size":20
}
text_format = workbook.add_format(format_dic)
#第一列------------------------------------------------
#第0行第0列内容为【Name】样式为 加粗,红色,大小20
worksheet.write(0,0,"姓名",text_format)
worksheet.write(1,0,"张三")
worksheet.write(2,0,"李四")
#第二列------------------------------------------------
#第0行第1列宽度为80
worksheet.set_column(0,1,40)
format_dic['num_format'] = "yyyy-mm-dd HH:MM:SS"

date_format = workbook.add_format(format_dic)

worksheet.write(0,1,"时间",text_format)
date_time = datetime.datetime.strptime("2013-01-03","%Y-%m-%d")
worksheet.write_datetime(1,1,date_time,date_format)

worksheet.write_datetime(2,1,datetime.datetime.now(),date_format)

workbook.close()

标签:format,新建,worksheet,xlsxwriter,Excel,datetime,write,workbook,date
来源: https://www.cnblogs.com/WenzhengLi/p/14321960.html