学习自《python编程快速上手——让繁琐的工作自动化》

其中一些方法和属性以改为新版的表达

挖坑待填(书中库太老……代码都不可用,有空更新)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import openpyxl

# 读取excel文档
wb = openpyxl.load_workbook('example.xlsx')

# 读取工作表
wb.sheetnames

# 选择工作表
sheet = wb[sheetname]

# 工作表名字
sheet.title

# 获取工作簿的活动表
wb.active

# 获取单元格及其值
sheet['A1']
sheet['A1'].value
sheet.cell(row = 1, column = 2)

# 获取单元格坐标值
c = sheet['A1']
c.row
c.column
c.coordinates

# 获取工作表大小
sheet.max_row
sheet.max_column
sheet.min_row
sheet.min_column

# 列字母和数字之间的转换
openpyxl.cell.column_index_from_string()
openpyxl.cell.get_column_letter()

# 从表中取得行和列
tuple(sheet['A1':'C3'])
sheet.columns[1]
sheet.rows[1]

# 创建并保存excel文档
wb = openpyxl.Workbook()
wb.save()

# 创建和删除工作表
wb.create_sheet(index, title)
wb.remove(sheet)

# 设置单元格字体风格
# font(name, size, bold, italic)
# 字体名,字体大小,字体加粗,字体斜体
from openpyxl.styles import Font, Style

italic24Font = Font(size = 24, italic = True)
styleObj = Style(font = italic24Font)
sheet['A1'].style = styleObj