White learn Python (24): Excel basic operations (under)

Life is short, I chose Python

The foregoing Portal

White learn Python (1): Opening

White Science Python (2): basic data type (on)

White Science Python (3): fundamental data types (lower)

White Science Python (4): Variable Basic Operation

White Science Python (5): base operator (on)

White Science Python (6): base operator (lower)

White Science Python (7): based flow control (on)

White Science Python (8): the basis of flow control (lower)

White Science Python (9): basic data structure (list) (a)

White Science Python (10): basic data structure (list) (lower)

White Science Python (11): basic data structure (tuples)

White Science Python (12): basic data structure (dictionary) (a)

White Science Python (13): basic data structure (dictionary) (lower)

White Science Python (14): basic data structure (set) (a)

White Science Python (15): basic data structure (set) (lower)

White Science Python (16): the basic data type (function) (a)

White Science Python (17): the basic data type (function) (lower)

White learn Python (18): basic file operations

White learn Python (18): basic file operations

White learn Python (19): base Exception Handling

White learn Python (20): iterator basis

White Science Python (21): base generator

White Science Python (22): time and simple to use calendar module

White learn Python (23): Excel Basic Operation (on)

Here Congratulations you see Benpian serialized students, Benpian serialized as "white school Python series basis," the last one, Congratulations taken a solid step on the road to learn Python.

Write Excel

First, of course, is to install third-party modules:

pip install openpyxl

First we need to create a WorkBook:

import xlsxwriter

workbook = xlsxwriter.Workbook('demo.xlsx')

Before all operations, we just need to remember to import the installation of xlsxwriterthe module.

Next, we create a Sheet:

sheet1 = workbook.add_worksheet('test_sheet')

Once created, you need to close the workbook, workbook this step will we just created is saved.

workbook.close()

Well, we have created an excel, the end of the operation, after class.

Teacher, you come back, this is over?

We then describe how to write data to Excel.

First, we can set the cell number format:

workfomat = workbook.add_format()
# 字体加粗
workfomat.set_bold(True)
# 单元格边框宽度
workfomat.set_border(1)
# 对齐方式
workfomat.set_align('left')
# 格式化数据格式为小数点后两位
workfomat.set_num_format('0.00')

Then we write content, too lazy to think for a small series of specific content, directly copy the contents of the article:

heads = ['', '语文', '数学', '英语']
datas = [
    ['小明', 76, 85, 95],
    ['小红', 85, 58, 92],
    ['小王', 98, 96, 91]
]

sheet1.write_row('A1', heads, workfomat)

sheet1.write_row('A2', datas[0], workfomat)
sheet1.write_row('A3', datas[1], workfomat)
sheet1.write_row('A4', datas[2], workfomat)

Then execute the program, we look at the results of the final output:

In addition to thus output outside, we can also specify the cell format output:

We list a more complex type of output date:

fomat1 = workbook.add_format({'num_format': 'yy/mm/dd/ hh:mm:ss'})

sheet1.write_datetime('E5', datetime.datetime(2019, 11, 9, 22, 44, 26), fomat1)

Note: The above format must be added, or displayed in Excel will only be a timestamp.

Other types of output here is not for example a small series, some common listed below:

# 字符串类型
sheet1.write_string()
# 数字型
sheet1.wirte_number()
# 空类型
sheet1.write_blank()
# 公式
sheet1.write_formula()
# 布尔型
sheet1.write_boolean()
# 超链接
sheet1.write_url()

We can also insert a picture in Excel, the sample is as follows:

sheet1.insert_image('I6', 'wx.jpg')

The syntax is as follows:

insert_image(row, col, image[, options])

row:行坐标,起始索引值为0;
col:列坐标,起始索引值为0;
image:string类型,是图片路径;
options:dict类型,是可选参数,用于指定图片位置,如URL等信息;

We can also draw a picture in Excel, including support area, bar, column, line charts, scatter plots.

Chart object is achieved by Workbook add_chart()a method created, which specifies the chart type:

chart = workbook.add_chart({'type': 'column'})

Common chart pattern as follows:

area:面积样式的图表
bar:条形图
column:柱状图
line:线条样式的图表
pie:饼形图
scatter:散点图
stock:股票样式的图表
radar:雷达样式的图表

Then use insert_chart()Worksheet method to insert it into the worksheet as an embedded chart:

sheet1.insert_chart('A7', chart)

Complete example as follows:

chart = workbook.add_chart({'type': 'column'})

chart.add_series({'values': '=test_sheet!$B$2:$B$4'})
chart.add_series({'values': '=test_sheet!$C$2:$C$4'})
chart.add_series({'values': '=test_sheet!$D$2:$D$4'})

sheet1.insert_chart('A7', chart)

The results are as follows:

Some commonly used simple operations described here, want to know more students access to official documents, links, small series have to find out: https://xlsxwriter.readthedocs.io/ .

Sample Code

This series of small series all the code will be placed on code management repository Github and Gitee, to facilitate access.

Sample Code -Github

Sample Code -Gitee

Guess you like

Origin www.cnblogs.com/babycomeon/p/11886983.html