2021-06-17

python自动化之Excel

Excel读取

读取对应表格

1.打开已经存在的Excel表格

from openpyxl import load_workbook
exl = load_workbook(filename = 'D:\\python自动化之excel\\test.xlsx')
print(exl.sheetnames)
['Sheet1', 'Sheet2', 'Sheet3']

2.根据名称获取表格

from openpyxl import load_workbook
exl = load_workbook(filename = 'D:\\python自动化之excel\\test.xlsx')
print(exl.sheetnames)
['Sheet1', 'Sheet2', 'Sheet3']
sheet = exl['Sheet1']
print(sheet)

3.获取Excel 内容占据的大小

from openpyxl import load_workbook
exl = load_workbook(filename = 'D:\\python自动化之excel\\test.xlsx')
print(exl.sheetnames)
['Sheet1', 'Sheet2', 'Sheet3']
sheet = exl['Sheet1']
print(sheet)

print(sheet.dimensions)
A1:A1

读取单元格

1.获取某个单元格的具体内容

from openpyxl import load_workbook
exl = load_workbook(filename = 'D:\\python自动化之excel\\test.xlsx')
print(exl.sheetnames)
['Sheet1', 'Sheet2', 'Sheet3']
sheet = exl['Sheet1']
print(sheet)
cell = sheet.cell(row=1,column=1)
print(cell.value)
['Sheet1', 'Sheet2', 'Sheet3']

Hello 

模拟实战

1.找出text.xlsx中sheet1表中空着的格子,并输出这些格子的坐标

from openpyxl import load_workbook
exl = load_workbook('D:\\python自动化之excel\\test.xlsx')
sheet = exl['Sheet1']
for row in sheet.iter_rows(min_row = 1, max_row = 10,
                           min_col = 1, max_col = 10):
    for cell in row:
        if not cell.value:
            print(cell.coordinate)

Excel写入

写入单元格并保存

1.写入单元格并保存

from openpyxl import load_workbook
exl = load_workbook('D:\\python自动化之excel\\test.xlsx')
sheet = exl['Sheet1']
sheet['A1'] = 'hello word'
exl.save(filename = 'D:\\python自动化之excel\\test.xlsx')
6cc7534b876f2050c7b05755e593f5c.png
写入行数据并保存

1.写入一行数据并保存

import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个sheet
sheet = workbook.add_sheet('My Worksheet')

# 写入excel
# 参数对应 行, 列, 值
sheet.write(1,0,label = 'this is test')

# 保存
workbook.save('D:\\python自动化之excel\\new_test.xls')

![
微信截图_20210617135144.png

]


微信截图_20210617135144.png

2.写入多行数据并保存
from openpyxl import load_workbook
exl = load_workbook('D:\\python自动化之excel\\test.xlsx')
sheet = exl['Sheet1']
sheet['A1'] = 'hello word'
exl.save(filename = 'D:\\python自动化之excel\\test.xlsx')
import xlwt
workbook = xlwt.Workbook(encoding = 'utf-8')
# 创建一个sheet
sheet = workbook.add_sheet('My Worksheet')
# 写入excel
data = [['hello',22,'hi'],
        ['hell',23,'h'],
        ['he',25,'him']]
for i in range(len(data)):
    for j in range(len(data[i])):
        sheet.write(i,j,data[i][j])
# 保存
workbook.save('D:\\python自动化之excel\\new2_test.xls')
微信截图_20210617135716.png

Sheet表操作

1.创建新的sheet

from openpyxl import load_workbook
exl = load_workbook(filename = 'D:\\python自动化之excel\\test.xlsx')
print(exl.sheetnames)
exl.create_sheet('new_sheet')
['Sheet1']
print(exl.sheetnames)
['Sheet1', 'new_sheet']

Excel 样式

设置字体样式

1.设置字体样式

from openpyxl import Workbook
from openpyxl.styles import Font
workbook = Workbook()
sheet = workbook.active
cell = sheet['A1']
font = Font(name='字体', size=10, bold=True, italic=True, color='FF0000')
cell.font = font
workbook.save(filename='D:\\python自动化之excel\\new3_test.xlsx')
image.png
设置对齐样式

1.设置单元格边框样式

from openpyxl import Workbook
from openpyxl.styles import Font,Side,Border
workbook = Workbook()
sheet = workbook.active
cell = sheet['A1']
side = Side(style='thin',color='FF000000')
#先定好side的格式
border = Border(left=side, right=side, top=side, bottom=side)
#代入边线中
cell.border = border
workbook.save(filename='D:\\python自动化之excel\\new_test4.xlsx')
image.png

2.设置行高与列宽

>>> from openpyxl import Workbook
... workbook = Workbook()
... sheet = workbook.active
... sheet.row_dimensions[1].height = 50
... sheet.column_dimensions['C'].width = 20
... workbook.save(filename='D:\\python自动化之excel\\new_test.xlsx')

模拟实战

1.打开test文件,找出文件中购买数量buy_mount超过5的行,并对其标红、加粗、附上边框。

from openpyxl import load_workbook
from openpyxl.styles import Font, Side, Border

workbook = load_workbook('D:\\python自动化之excel\\test.xlsx')
sheet = workbook.active
buy_mount = sheet['F']
row_lst = []

for cell in buy_mount:
    if isinstance(cell.value, int) and cell.value > 5:
        print(cell.row)
        row_lst.append(cell.row)

side = Side(style='thin', color='FF000000')
border = Border(left=side, right=side, top=side, bottom=side)
font = Font(bold=True, color='FF0000')
for row in row_lst:
    for cell in sheet[row]:
        cell.font = font
        cell.border = border
workbook.save('D:\\python自动化之excel\\new.xlsx')
image.png

你可能感兴趣的:(2021-06-17)