Python学习笔记(十)——处理Excel电子表格

LibreOffice
Apache OpenOffice

安装openpyxl模块

pip install openpyxl

openpyxl使用手册

读取Excel文件

用openpyxl模块打开Excel文档

>>> wb = openpyxl.load_workbook('temp.xlsx')
>>> type(wb)
<class 'openpyxl.workbook.workbook.Workbook'>

从工作簿取得工作表

get_sheet_names()
get_sheet_by_name('Sheet3')
get_sheet_by_name('Sheet3')

>>> wb.get_sheet_names()
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name('Sheet3')
>>> sheet
"Sheet3">
>>> type(sheet)
<class 'openpyxl.worksheet.worksheet.Worksheet'>
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.get_sheet_by_name('Sheet3')
>>> anotherSheet
"Sheet3">

从表中获取单元格

>>> import openpyxl
>>> wb = openpyxl.load_workbook('temp.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet3')
>>> type(sheet)
<class 'openpyxl.worksheet.worksheet.Worksheet'>
>>> sheet['B1']
'Sheet3'.B1>
>>> sheet['B1'].value
'10点06分'
>>> '现在是' + str(sheet['C1'].value) + '上午' + str(sheet['B1'].value)
'现在是43141上午10点06分'
>>> '现在是' + str(sheet['C1'].value) + '上午' + str(sheet['B1'].value)
'现在是43141上午10点06分'
>>> 'ROW' + str(sheet['B1'].row) + 'Column ' + str(sheet['B1'].column) + 'is' + str(sheet['B1'].value)
'ROW1Column Bis10点06分'
>>> 'ROW ' + str(sheet['B1'].row) + ' Column ' + str(sheet['B1'].column) + ' is ' + str(sheet['B1'].value)
'ROW 1 Column B is 10点06分'
>>> sheet['B1'].coordinate
'B1'
>>> sheet.cell(row=1,column=2)
'Sheet3'.B1>
>>> sheet.cell(row=1,column=2).value
'10点06分'
>>> for i in range(1,8,2):
    print(i,sheet.cell(row = i , column = 2).value)


1 10063 10085 10107 1012
>>> sheet.max_row
7
>>> sheet.max_column
4

列表字母转换

>>> import openpyxl
>>> try:
    from openpyxl.cell import get_column_letter,column_index_from_string
except ImportError:
    from openpyxl.utils import get_column_letter,column_index_from_string

>>> get_column_letter(27)
'AA'
>>> wb = openpyxl.load_workbook('temp.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet3')
>>> get_column_letter(sheet.max_column)
'D'
>>> column_index_from_string('AAA')
703

get_column_letter’

从表中获取行和列

>>> wb = openpyxl.load_workbook('temp.xlsx')
>>> sheet = wb.get_sheet_by_name('Sheet3')

Warning (from warnings module):
  File "__main__", line 1
DeprecationWarning: Call to deprecated function get_sheet_by_name (Use wb[sheetname]).
>>> tuple(sheet['B1':'D7'])
((<Cell 'Sheet3'.B1>, <Cell 'Sheet3'.C1>, <Cell 'Sheet3'.D1>), (<Cell 'Sheet3'.B2>, <Cell 'Sheet3'.C2>, <Cell 'Sheet3'.D2>), (<Cell 'Sheet3'.B3>, <Cell 'Sheet3'.C3>, <Cell 'Sheet3'.D3>), (<Cell 'Sheet3'.B4>, <Cell 'Sheet3'.C4>, <Cell 'Sheet3'.D4>), (<Cell 'Sheet3'.B5>, <Cell 'Sheet3'.C5>, <Cell 'Sheet3'.D5>), (<Cell 'Sheet3'.B6>, <Cell 'Sheet3'.C6>, <Cell 'Sheet3'.D6>), (<Cell 'Sheet3'.B7>, <Cell 'Sheet3'.C7>, <Cell 'Sheet3'.D7>))
>>> for temp in sheet['B1':'D7']:
    for tempp in temp:
        print(tempp.coordinate,tempp.value)
    print('---END---')


B1 10点06分
C1 二〇一八年二月十日
D1 75
---END---
B2 10点07分
C2 二〇一八年二月十日
D2 80
---END---
B3 10点08分
C3 二〇一八年二月十日
D3 85
---END---
B4 10点09分
C4 二〇一八年二月十日
D4 90
---END---
B5 10点10分
C5 二〇一八年二月十日
D5 95
---END---
B6 10点11分
C6 二〇一八年二月十日
D6 100
---END---
B7 10点12分
C7 二〇一八年二月十日
D7 105
---END---
>>> sheet['A']
(<Cell 'Sheet3'.A1>, <Cell 'Sheet3'.A2>, <Cell 'Sheet3'.A3>, <Cell 'Sheet3'.A4>, <Cell 'Sheet3'.A5>, <Cell 'Sheet3'.A6>, <Cell 'Sheet3'.A7>)
>>> sheet['1']
(<Cell 'Sheet3'.A1>, <Cell 'Sheet3'.B1>, <Cell 'Sheet3'.C1>, <Cell 'Sheet3'.D1>)
>>> 

list(sheet.columns)[1]

小实验——从电子表格中读取数据

#! python3
#readExcel.py 
#
#Author : qmeng
#MailTo : [email protected]
#QQ     : 1163306125
#Blog   : http://blog.csdn.net/Mq_Go/
#Create : 2018-02-010
#Version: 1.0
#
import openpyxl,pprint,os
print('Opening workbook...')
wb = openpyxl.load_workbook('2017.xlsx')
sheet = wb.get_sheet_by_name('Sheet0')
Data = {}
print('Reading rows...')
for row in range(3,sheet.max_row+1):
    Num      = sheet['B'+str(row)].value
    Name     = sheet['E'+str(row)].value
    sProject = sheet['F'+str(row)].value
    eProject = sheet['G'+str(row)].value

    #填充数据结构
    #Data.setdefault(Num,{Name:'',sProject:'',eProject:''})
    Data[Num] = {'Name':Name,'sProject':sProject,'eProject':eProject}
    #print(Name + ' 同学信息加载完毕...')
print('Writing results...')
File = open('abcdef.py','w')
File.write('allData = ' + pprint.pformat(Data))
File.close()
print('Done...')

#import abcdef
#>>> abcdef.allData['2017000349']
#{'Name': '18834198699', 'eProject': '9/933', 'sProject': 4.28}

写入Excel文档

简书

>>> import openpyxl
>>> wb = openpyxl.Workbook()
>>> sheet = wb.get_active_sheet()
>>> sheet.title
'Sheet'
>>> sheet.title = 'first'
>>> sheet['A1'] = '123456'
>>> wb.save('example.xlsx')

小实验——更新一个电子表格

#! python3
#updateProduce.py - 
#Usage:
#
#Author : qmeng
#MailTo : [email protected]
#QQ     : 1163306125
#Blog   : http://blog.csdn.net/Mq_Go/
#Create : 2018-02-10 15:42:18
#Version: 1.0
#
import openpyxl
wb = openpyxl.load_workbook('temp.xlsx')
sheet = wb.get_sheet_by_name('Sheet3')
UPDATA = {'10点06分':'200',
            '10点07分':'300',
            '10点08分':'400'}
print(sheet.max_row)
for row1 in range(sheet.max_row):
        if sheet.cell(row = row1,column = 2)value in UPDATA.keys():
                sheet['D'+str(roe)] = UPDATA[sheet['B'+str(row)].value]
                print(sheet['B'+str(row)].value)
wb.save('temp.xlsx')

你可能感兴趣的:(Python)