Python基础 - Excel导入导出

Python基础 - Excel导入导出

 ​Excel文件是Microsoft Excel用来存储电子表格的文件格式,对Excel的处理方式与带分隔符的文件非常相似。由于Excel可以读写csv文件,因此从Excel电子表格文件中提取数据的最快捷、最简单的方法,其实往往是在Excel中打开并另存为csv文件。同一个文件包含多张表格、宏、多种单元格式等。事实上,Python的标准库中并没有包含读写Excel文件的模块,需要安装外部模块才能读取Excel格式。
​ 本文使用OpenPyXL的模块来处理Excel,通过pip install openpyxl命令安装。读取文件相当简单,但还是比csv文件要繁琐一些。首先要载入工作簿,然后要找到指定的表格,然后就可以遍历每一行,开始提取各个单元格中的数据。OpenPyXL不支持老版本的xls格式。
 openpyxl模块有三大类,Workbook是对工作簿的抽象,Worksheet是对表格的抽象,Cell是对单元格的抽象。

# Excel文件的读取
from openpyxl import load_workbook
>>> wb = load_workbook('test.xlsx')
>>> results = []
>>> ws = wb.worksheets[0]
>>> for row in ws.iter_rows():
	results.append([cell.value for cell in row])
>>> print(results)
[[1, '《阿河》', '朱自清', '朱自清《阿河》', datetime.datetime(2020, 4, 13, 5, 16, 6), datetime.datetime(2020, 4, 13, 5, 16, 9)], [2, '《论自己》', '朱自清', '朱自清《论自己》', datetime.datetime(2020, 4, 13, 5, 17, 7), datetime.datetime(2020, 4, 13, 5, 17, 11)]]

 如果电子表格包含的格式具有某种重要含义,如果标签需要忽略或单独处理,如果需要处理公式和引用,就需要深入研究这些部分的处理方式,并且需要编写更加复杂的代码。电子表格通常被限制在约一百万行的规模。建议尽量采用csv或带分隔符的文件,通常没有必要容忍电子表格带来的额外复杂性和格式处理的麻烦。
 电子表格文件的写入,与读取过程类似。首先需要创建工作簿或电子表格文件,然后要创建一张或多张表,最后把数据写入合适的单元格中。当然可以由csv数据文件新建电子表格。

# Excel文件的写入
>>> data_rows = [
	['刘备', '创业者'],
	['关羽', '高级主管'],
	['张飞', '高级主管'],
	['诸葛亮', '最强打工人']
	]
>>> from openpyxl import Workbook
>>> wb = Workbook() # 实例化工作簿
>>> ws = wb.active  # 激活worksheet
>>> ws.title = 'MS' # 设置worksheet名称
>>> for row in range(len(data_rows)):
	ws.append(data_rows[row])
>>> wb.save('abc.xlsx') #保存工作簿
  1. python操作Excel写数据

(1)创建、删除工作表

>>> from openpyxl import Workbook
# 创建工作簿
>>> wb = Workbook()
>>> ws = wb.active
>>> print('默认工作表名称', ws.title)
默认工作表名称 Sheet
>>> ws.title = 'mySheet'
# 获取工作簿的所有工作表名称
>>> print(wb.sheetnames)
['mySheet']
>>> print(wb.get_sheet_names)
>>> print('创建工作表')
>>> wb.create_sheet(index = 1, title = 'One Sheet')
<Worksheet "One Sheet">
>>> wb.create_sheet(index = 2, title = 'Two Sheet')
<Worksheet "Two Sheet">
>>> wb.create_sheet(index = 3, title = 'Three Sheet')
<Worksheet "Three Sheet">
>>> print(wb.sheetnames)
['mySheet', 'One Sheet', 'Two Sheet', 'Three Sheet']
>>> print('删除工作表')
>>> wb.remove(wb['Two Sheet'])

(2)使用list写入

from openpyxl import Workbook
wb = Workbook()
ws = wb.create_sheet(title='list写入')
print(wb.sheetnames)
rows = [
    ['星期一''星期二', '星期三', '星期四', '星期五', '星期六', '星期日'],
    [1, 2, 3, 4, 5, 6, 7],
    [4, 2, 3, 4, 2, 3, 3],
    [5, 2, 4, 4, 2, 1, 3]
]
for row in rows:
    ws.append(row)

(3)使用range方式写入

>>> ws = wb.create_sheet('Range Sheet')
>>> for row in range(1, 30):
	ws.append(range(15))

(4)使用cell()方法写入

>>> ws = wb.create_sheet('Cell Sheet')
>>> from openpyxl.utils import get_column_letter # 根据当前列获取该列名称
# 指定数据写入的行列
>>> for row in range(10, 20):
	for col in range(5, 15):
		ws.cell(row = row, column=col, value=get_column_letter(col))
  1. python操作Excel读数据

(1)打开工作簿,获取所有工作表名称

from openpyxl import load_workbook
wb = load_workbook('demo.xlsx')
# 获取所有的工作表名称
print(wb.sheetnames)
# 获取当前激活的工作表
print(wb.active.title)
# 通过工作簿获取所有的工作表名称
for s in wb:
    print(s.title)

(2)创建工作表

wb = load_workbook('demo.xlsx')
mySheet = wb.create_sheet('mySheet')
print(mySheet)

(3)根据工作名获取工作表

sheet = wb.get_sheet_by_name('mySheet')
sheet = wb['mySheet']

(4)获取单元格对象及单元格的值

wb = load_workbook('demo.xlsx')
ws = wb.active
print(ws['A1'])
print(ws['A1'].value)

(5)获取单元格的行列值

wb = load_workbook('demo.xlsx')
ws = wb.active
# 根据单元格名称获取单元格对象
c = ws['C3']
print('row:{}, column:{}, value:{}'.format(c.row, c.column, c.value))
print('coordinate:', c.coodrinate)
print(ws.cell(row=2, column=2).value)
print('循环遍历获取:')
for r in ws.rows:
    for c in r:
        print(c.value, end='\t\t')
    print()
  1. python操作Excel修改样式

(1)修改字体样式

from openpyxl import Workbook
from openpyxl.styles import colors, Font
wb = Workbook()
ws = wb.active
ws.title = '修改字体样式'
f_font = Font(name='宋体', size=24, italic=True)
ws['C2'].font = f_font
ws['C2'] = '宋体 24 italic'

(2)设置单元格公式

from openpyxl import Workbook
from openpyxl.styles import colors, Font
wb = Workbook()
ws = wb.active
ws = wb.create_sheet('单元格公式')
ws['B1'] = 200
ws['B2'] = 100
ws['B3'].font = Font('微软雅黑', size=18, bold=True, color=colors.BLUE)
ws['B3'] = '=SUM(B1:B2)'

(3)设置行高和列宽

from openpyxl import Workbook
from openpyxl.styles import colors, Font
wb = Workbook()
ws = wb.active
ws = wb.create_sheet('维度')
ws['A1'] = 'HaLuo'
ws.row_dimensions[1].height = 80
ws['B2'] = 'Hello'
ws.column_dimensions['B'].width = 30

(4)合并单元格

from openpyxl import Workbook
from openpyxl.styles import colors, Font
wb = Workbook()
ws = wb.active
ws = wb.create_sheet('合并单元格')
ws.merge_cells('A1:D3')
ws['A1'] = 'This is a cute cat.'
ws.merge_cells('C5:D5')
ws['C5'] = 'Merged Cells'

(5)拆分单元格

from openpyxl import Workbook
from openpyxl.styles import colors, Font
wb = Workbook()
ws = wb.active
ws = wb.copy_worksheet(wb.get_sheet_by_name('合并单元格'))
ws.title = '未合并单元格'
ws.unmerge_cells('A1:D3')
ws.unmerge_cells('C5:D5')
  1. python操作Excel图表

(1)饼图

from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference, BarChart, BubbleChart, ScatterChart, Series
# 准备数据
rows = [
    ['Pie', 'Sold'],
    ['Apple', 25],
    ['Xiaomi', 25],
    ['Huawei', 25],
    ['Oppo', 25]
]

wb = Workbook()
ws = wb.active
ws.title = '饼图'
for row in rows:
    ws.append(row)
# 绘制饼图    
pie_chart = PieChart()
pie_chart.title = '饼图标题'
# 进行分类,数据在第二列
category = Reference(ws, min_col=1, min_row=2, max_row=5)
data = Reference(ws, min_col=2, min_row=2, max_row=5)
pie_chart.add_data(data)
pie_chart.set_categories(category)
ws.add_chart(pie_chart, 'D2')
wb.save('饼图.xlsx')

(2)条形图和柱形图

​ 在条形图中,值被绘制成水平条或垂直条,通过type属性来设置。

from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference, BarChart, BubbleChart, ScatterChart,Series  # Reference:图标所用信息

wb = Workbook()
# 绘制柱状图
ws = wb.create_sheet('条形图')
rows = [
    ('第1列', '第2列', '第3列'),
    (2, 10, 30),
    (3, 40, 60),
    (4, 50, 70),
    (5, 20, 10),
    (6, 10, 40),
    (7, 50, 30),
]

for row in rows:
    ws.append(row)
# 绘制柱状图
bar_chart = BarChart()
bar_chart.type = 'col'  # col垂直、水平柱状图 bar
bar_chart.title = '条形图'
bar_chart.style = 10  # 设置颜色,10的对比度最强,红色与蓝色
bar_chart.x_axis.title = '示例长度(mm)'
bar_chart.y_axis.title = '测试数值'
# 设置分类
category = Reference(ws, min_col=1, min_row=2, max_row=7)
# 获取数据
data = Reference(ws, min_col=2, max_col=3, min_row=1, max_row=7)
# 柱状图对象添加数据
bar_chart.add_data(data, titles_from_data=True) 
bar_chart.set_categories(category)
ws.add_chart(bar_chart, 'E1')
wb.save('条形图.xlsx')

你可能感兴趣的:(python,python,开发语言)