from openpyxl import load_workbook
def open_workbook(path):
workbook = load_workbook(filename=path)
print(f'工作表名: {workbook.sheetnames}')
sheet = workbook.active
print(sheet)
print(f'當前工作表名是:{sheet.title}')
if __name__ == '__main__':
open_workbook(r'C:\test\myfilebook.xlsx')
from openpyxl import load_workbook
def get_cell_info(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
print(sheet)
print(f'當前工作表名稱為:{sheet.title}')
print(f'單元格A2值{sheet['A2'].value=}')
print(f'單元格A3值{sheet['A3'].value=}')
cell = sheet['B3']
print(f'{cell.value=}')
if __name__ == '__main__':
get_cell_info(r'C:\test\myfilebook.xlsx')
def get_info_by_coord(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
cell = sheet['A2']
print(f'行 {cell.row}, 列{cell.column} = {cell.value}')
print(f'{cell.value=}位于{cell.coordinate=}')
if __name__ == '__main__':
get_info_by_coord(r'C:\test\myfilebook.xlsx')
from openpyxl import load_workbook
def iterating_range(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
for cell in sheet['A']:
print(cell)
if __name__ == '__main__':
iterating_range(r'C:\test\myfilebook.xlsx')
from openpyxl import load_workbook
def iterating_over_values(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
for value in sheet.iter_rows(
min_row=1, max_row=3,
min_col=1, max_col=3,
values_only=True,):
print(value)
if __name__ == '__main__':
iterating_over_values(r'C:\test\myfilebook.xlsx')
from openpyxl import Workbook
def create_workbook(path):
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = '完美Excel'
sheet['A2'] = 'excelperfect'
sheet['A3'] = 'Excel'
workbook.save(path)
if __name__ == '__main__':
create_workbook(r'C:\test\openpyxl.xlsx')
import openpyxl
def create_worksheets(path):
workbook = openpyxl.Workbook()
print(workbook.sheetnames)
#添加新工作表
workbook.create_sheet()
print(workbook.sheetnames)
#插入工作表
workbook.create_sheet(index=1,title='Second sheet')
print(workbook.sheetnames)
workbook.save(path)
if __name__ == '__main__':
create_worksheets(r'C:\test\mynewsheets.xlsx')
import openpyxl
def create_worksheets(path):
workbook = openpyxl.Workbook()
workbook.create_sheet()
# 插入工作表
workbook.create_sheet(index=1, title='Second sheet')
print(workbook.sheetnames)
del workbook['Second sheet']
print(workbook.sheetnames)
workbook.save(path)
if __name__ == '__main__':
create_worksheets(r'C:\test\del_sheets.xlsx')
import openpyxl
def remove_worksheets(path):
workbook = openpyxl.Workbook()
sheet1 = workbook.create_sheet()
# 插入工作表
workbook.create_sheet(index=1,
title='Second sheet')
print(workbook.sheetnames)
workbook.remove(sheet1)
print(workbook.sheetnames)
workbook.save(path)
if __name__ == '__main__':
remove_worksheets(r'C:\test\remove_sheets.xlsx')
from openpyxl import Workbook
def inserting_cols_rows(path):
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = '完美Excel'
sheet['A2'] = 'excelperfect'
sheet['A3'] = 'Excel'
# 在列A前插入一列
sheet.insert_cols(idx=1)
# 第2行開始插入2行
sheet.insert_rows(idx=2, amount=2)
workbook.save(path)
if __name__ == '__main__':
inserting_cols_rows(r'C:\test\inserting.xlsx')
from openpyxl import Workbook
def deleting_cols_rows(path):
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = '完美Excel'
sheet['B1'] = 'excelperfect'
sheet['C1'] = 'Excel'
sheet['A2'] = '行2'
sheet['A3'] = '行3'
sheet['A4'] = '行4'
# 刪除列A
sheet.delete_cols(idx=1)
# 從第2行開始刪除2行
sheet.delete_rows(idx=2, amount=2)
workbook.save(path)
if __name__ == '__main__':
deleting_cols_rows(r'C:\test\deleting.xlsx')
聯(lián)系客服