五、处理excel 表格
1、安装第三方模块openpyxl
cmd 命令窗口输入:
pip install openpyxl
2、D:\python目录下,新建demo.xlsx文件,并编辑内容如下
苹果 | 红色 | 5元/斤 |
---|
香蕉 | 黄色 | 3元/斤 | 橘子 | 橙色 | 6元/斤 | 柚子 | 黄色 | 5元/斤 |
3、读取Excel文档
1)openpyxl.load_workbook(‘文件名’):获取workbook对象
此时,这里的workbook对象就代表传入文件名的Excel文件
>>> import openpyxl
>>> excel = openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>> type(excel)
<class 'openpyxl.workbook.workbook.Workbook'>
2)从该Excel文件工作簿中获取工作列表
>>> import openpyxl
>>> excel = openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>>
>>> excel.sheetnames
['Sheet1', 'Sheet2', 'Sheet3']
>>>
>>> sheet1=excel['Sheet1']
>>> sheet1.title
'Sheet1'
>>> type(sheet1)
<class 'openpyxl.worksheet.worksheet.Worksheet'>
>>>
>>> sheetActive=excel.active
>>> sheetActive.title
'Sheet1'
>>> type(sheetActive)
<class 'openpyxl.worksheet.worksheet.Worksheet'>
3)从表中获取单元格的基本信息
>>> import openpyxl
>>> excel=openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>>
>>> sheet1=excel['Sheet1']
>>>
>>> sheet1['A1'].row
1
>>>
>>> sheet1['A1'].column
1
>>>
>>> sheet1['A1'].value
'苹果'
>>>
>>> sheet1.max_row
4
>>>
>>> sheet1.max_column
3
4)列标:字母和数字之间的转换
>>>
>>> import openpyxl
>>>
>>> from openpyxl.utils import get_column_letter,column_index_from_string
>>>
>>> get_column_letter(4)
'D'
>>>
>>> column_index_from_string('DD')
108
5)通过Worksheet对象切片,取得电子表格中的行和列
>>>
>>> import openpyxl
>>> excel = openpyxl.load_workbook('D:\\python\\demo.xlsx')
>>>
>>> sheet1=excel['Sheet1']
>>> for rows in sheet1['A1':'B2']:
... for cell in rows:
... print(cell.value)
...
苹果
红色
香蕉
黄色
4、写入Excel文档
1)创建并保存Excel文档
>>>
>>> import os
>>>
>>> os.listdir('D:\\python')
['demo', 'demo.xlsx', 'error.txt', 'mylog1.txt', 'new', 'new.zip', 'test']
>>>
>>> import openpyxl
>>>
>>> excel = openpyxl.Workbook()
>>>
>>> excel.sheetnames
['Sheet']
>>>
>>> sheet=excel['Sheet']
>>>
>>> sheet.title
'Sheet'
>>>
>>> sheet.title='NewSheet'
>>> sheet.title
'NewSheet'
>>>
>>> excel.save('D:\\python\\newExcelDemo.xlsx')
>>>
>>> os.listdir('D:\\python')
['demo', 'demo.xlsx', 'error.txt', 'mylog1.txt', 'new', 'new.zip', 'newExcelDemo.xlsx', 'test']
>>>
newExcelDemo.xlsx 如图:
2)在刚新建的工作簿中,进行创建和删除工作表的操作
>>>
>>> import openpyxl
>>>
>>> excel=openpyxl.load_workbook('D:\\python\\newExcelDemo.xlsx')
>>>
>>> excel.sheetnames
['NewSheet']
>>>
>>>
>>> excel.create_sheet()
<Worksheet "Sheet">
>>> excel.sheetnames
['NewSheet', 'Sheet']
>>>
>>> excel.create_sheet(title='Sheet1')
<Worksheet "Sheet1">
>>> excel.sheetnames
['NewSheet', 'Sheet', 'Sheet1']
>>>
>>> excel.create_sheet(index=0)
<Worksheet "Sheet2">
>>> excel.sheetnames
['Sheet2', 'NewSheet', 'Sheet', 'Sheet1']
>>>
>>> excel.create_sheet(index=1,title='Sheet333')
<Worksheet "Sheet333">
>>> excel.sheetnames
['Sheet2', 'Sheet333', 'NewSheet', 'Sheet', 'Sheet1']
>>>
>>>
>>> excel.remove(excel['Sheet333'])
>>> excel.sheetnames
['Sheet2', 'NewSheet', 'Sheet', 'Sheet1']
>>>
>>> del excel['NewSheet']
>>> excel.sheetnames
['Sheet2', 'Sheet', 'Sheet1']
>>>>>>
>>> excel.save('D:\\python\\newExcelDemo.xlsx')
>>>
newExcelDemo.xlsx 如图:
3)在刚新建的工作簿中,Sheet工作表中写入值
>>>
>>> import openpyxl
>>> excel=openpyxl.load_workbook('D:\\python\\newExcelDemo.xlsx')
>>> excel.sheetnames
['Sheet2', 'Sheet', 'Sheet1']
>>>
>>> excel.active
<Worksheet "Sheet2">
>>>
>>> excel.active=excel['Sheet']
>>> excel.active
<Worksheet "Sheet">
>>>
>>> sheet=excel.active
>>> sheet
<Worksheet "Sheet">
>>>
>>> sheet['A1'].value='你好'
>>> sheet['A1'].value
'你好'
>>>
>>> sheet['B1']='世界'
>>> sheet['B1']
<Cell 'Sheet'.B1>
>>> sheet['B1'].value
'世界'
>>>
>>> excel.save('D:\\python\\newExcelDemo.xlsx')
newExcelDemo.xlsx 如图:
5、设置单元格的字体风格
(1)引入openpyxl模块,并导入openpyxl.styles模块中的Font()函数和Style()函数
import openpyxl
from openpyxl.styles import Font
(2)新建一个xlsx文件或打开一个xlsx文件,这里选择打开一个存在的demo.xlsx文件,如下图:
excel = openpyxl.load_workbook('D:\\python\\demo.xlsx')
(3)获取demo.xlsx文件中,工作表Sheet1
sheet = excel['Sheet1']
(4)设置苹果字体大小为20,设置‘红色’字体颜色为红色,并加粗
font1 = Font(size=20)
font2 = Font(bold=True,color='FF0000')
sheet['A1'].font=font1
sheet['B1'].font=font2
(5)保存编辑后的文档
excel.save('D:\\python\\demo.xlsx')
(6)打开demo.xlsx,结果如图:
如需了解更多,请前往openpyxl官方网站,查看最新、最详细的说明文档。
|