1. Wookbook
Workbook (filename, [options]) The
Workbook class represents the entire spreadsheet file and is stored on the disk.
Parameter analysis:
filename (String type) is to create an Excel file storage path
option (Dict type) is an optional Workbook parameter, generally used as the initial worksheet content format, for example {'strings_to_numbers': True} means use worksheet.write () method The string is activated to convert numbers.
1.1, add_worksheet ([sheetname]) method
Function: add a new worksheet
Parameter analysis: sheetname (String type) is an optional worksheet name, the default is Sheet1
worksheet1 = workbook.add_worksheet() #Sheet1
worksheet2 = workbook.add_worksheet("表1") #表1
worksheet3 = workbook.add_worksheet() #sheet3
worksheet4 = workbook.add_worksheet("data") #data
1.2, add_format ([properties]) method
Function: Create a new format object in the worksheet to format the cell
Parameter analysis: properties (dict type) is a dictionary that specifies a format property
设置加粗格式对象,workbook.add_format({"blod":True})
通过Format methods(格式化方法)也可实现与字体加粗效果
bold = workbook.add_format({"bold":True}) #方法一
bold2 = workbook.add_format() #方法二
bold2.set_bold() #方法二
1.3, add_chart (option) method
Function: create a chart object in the worksheet, internally through the insert_chart () method to achieve
parameter analysis: options (dict type) specify a dictionary attribute for the chart
例如:设置一个线条类型的图标对象,workbook.add_chart({"type":"line"})
chart = workbook.add_chart({"type":"line"})
1.4, close () method
Function: close the worksheet file
workbook.close()
2. The Worksheet class
represents an Excel worksheet and cannot be instantiated directly. It needs to be created by calling the add_worksheet () method through the Workbook object.
worksheet = xlsxwriter.Workbook('test.xlsx').add_worksheet() #创建Sheet1工作表对象
worksheet = xlsxwriter.Workbook('test.xlsx').add_worksheet("data") #创建data工作表对象
2.1, write (row, col, * args) method
Function: write ordinary data to the cell
parameter analysis of the worksheet : the parameter row is the row coordinate; col is the column coordinate; the starting value of the coordinate index is 0; * args is the data content , Can be a
write method for numbers, formulas, strings or format objects has been used as an alias for other more specific data types, including:
write_string () write string type data
worksheet.write_string(0,0,"Your text here")
write_number () write number type data
worksheet.write_number('A2',2.345)
write_blank () write empty data
worksheet.write("A2",None)
write_formula () write formula type data
worksheet.write_formula(2,0,"=SUM(B1:B5)")
write_datetime () write date type data
import datetime
worksheet.write_datetime(7 , 0 , datetime.datetime.strptime('2018-07-31','%Y-%m-%d'),workbook.add_format({'num_format':'yyyy-m-dd'}))
write_boolean () write logic type data
worksheet.write_boolean(0 , 0 , True)
write_url () write hyperlink type data
worksheet.write_url("A1","ftp://www.python.org/")
Correspondence between write method and alias
worksheet.write(0 , 0 , 'hello') #对应write_string()
worksheet.write(1, 0 , 3.002) #write_number()
worksheet.write(2 , 0 , '=SUM(A1:A2)') #write_formula()
worksheet.write(3 , 0 , '') #write_blank()
worksheet.write(4, 0 , None) #write_blank()
2.2, set_row (row, height, cell_format, options)
method role: Set the row cell attribute
parameter analysis: row (int type) row position, starting subscript is 0; height (float type) row height, unit pixels; cell_format (format type) specifies the format object; options (dict type) sets the line hidden (hidden), level (combined rating), collapsed (collapsed)
worksheet.write("A1","hello") #在A1单元格写入"hello"字符串
cell_format = workbook.add_format({"bold":True}) #定义一个加粗的格式对象
worksheet.set_row(0 , 40 , cell_format) #设置第1行单元格高度40像素,且引用加粗格式对象
worksheet.set_row(1,None,None,{"hidden":True}) #隐藏第2行单元格
2.3. Set_column (first_col, last_col, width, cell_format, options)
method role: Set one or more columns of cell attribute
parameter analysis: first_col (int type), start column position, start subscript 0; last_col (int type) end Column position, starting subscript is 0; width (float type) sets column width; cell_format (Format type) format object; options (dict type) line hidden (hidden), level (combined grading), collapse (collapse)
worksheet.write('A1' , 'hello') #在A1单元格写入"hello"字符串
worksheet.write('B1',"world") #在B1单元格写入“world”字符串
cell_format = workbook.add_format({"bold":True}) #定义一个加粗的格式对象
worksheet.set_column(0 , 1 , 10 , cell_format) #设置0到1列单元格宽度为10像素,且引用加粗格式对象
worksheet.set_column("C:D", 20) #设置C到D列单元格宽度20像素
worksheet.set_column("E:G" , None , None , {"hidden":1}) #隐藏E到G列单元格
2.4, insert_image (row, col, image [, options])
method role: insert an image into a specified cell, support PNG, JPEG, BMP and other image format
parameter analysis: row (int type) row coordinates; col (int type) column Coordinates; coordinate index value is 0; image (string type) image path; options (dict type) optional parameters, specifying the location, scale, link URL and other information of the image
#在B5单元格插入python-logo.png图片,图片超链接http://python.org
worksheet.insert_image("B5",'img/python-logo.png',{'url':'http://python.org'})
3.Chart
statement: chart = xlswriter.Workbook (“a.xlsx”). Add_chart ({type, 'chart type'})
Support icon types include area, bar chart, column chart, line chart, pie chart, Scatter
charts , stocks, radar, etc. Chart type:
area: create an area style chart
bar: create a bar style chart
column: create a column style chart
line: create a line style chart
pie: create a pie chart Style chart
scatter: create a scatter style chart
stock: create a stock style chart
radar: create a radar style chart
chart = xlswriter.Workbook("a.xlsx").add_chart({type,'column'})
worksheet.insert_chart('A7',chart) #在A7单元格插入图表
3.1 The
role of char.add_series (options) method: add a data series to the chart
Parameter analysis: options (dict type) set the dictionary of chart series options
char.add_series
({
'categories':'=Sheet1!$A$1:$A$5', #categories指图表类型标签范围
'values':"=Sheet1!$B$1:$B$5", #values设置图表数据范围
'line':{'color':'red'}, #line设置图表线条属性,包括颜色,宽度等
})
3.2 The
role of the set_x_axis (options) method: set the chart X-axis options
chart.set_x_axis
({
'name':'月销售额', #设置x轴标题名称
'name_font':{'size':14,'bold':True}, #设置X轴标题字体属性
"num_font":{'italic':True}, #设置x轴数字字体属性
})
3.3 The
role of set_size (options) method: set the chart size
chart.set_size({"width":720,"height":576}) #width为宽度,height为高度
3.4 The
role of set_title (options) method: set the chart title
chart.set_titile({"name":"每月销售统计"})
3.5 The
role of set_style (style_id) method: set the chart style
Parameter analysis: style_id for different numbers represents different styles
chart.set_style(37) #37代表37样式
3.6. The role of set_table (options)
method: Set the X axis to data table format
chart.set_table()
4. Simple case
import xlsxwriter
workbook = xlsxwriter.Workbook("demo1.xlsx") #创建一个Excel文件
bold = workbook.add_format({"bold":True}) #定义一个加粗的格式对象 worksheet.set_column('A:A',20) #设定第一列(A)宽度为20像素
bold = workbook.add_format({"bold":True}) #定义一个加粗的格式对象
worksheet.write('A1','hello')
worksheet.write('A2','World',bold) #A2单元格写入"World"并引用加粗格式对象boldworksheet.write('B2',u'中文测试',bold) #B2单元格写入中文并引用加粗格式对象bold worksheet.write(2,0,32) #第2行,第0列,填入数字32
worksheet.write(3,0,35.5) #第3行,第0 列,填入数字35.5
worksheet.write(4,0,'=SUM(A3:A4)') #在第4行,第0列,计算A3:A4的和 worksheet.insert_image('B5','img/书.jpg') #在B5位置插入图片
workbook.close() #关闭Excel文件,同时意味着保存数据