用 Python 帮运营妹纸快速搞定 Excel 文档
Python中文社区
共 9534字,需浏览 20分钟
·
2020-11-17 04:12
Spreadsheet(电子表格) 或者 Workbook(工作簿) – 指文件本身(.xls or .xlsx). Worksheet(工作表) 或者 Sheet(表)–工作簿中的单个内容表,电子表格可以包含多个工作表。 Column(列) – 用英文字母标记的垂直数列,以“ A”开头。 Row(行) – 从1开始以数字标记的水平数列。 Cell(单元格) – 列和行的组合,例如“ A1”。
Python 读写 Excel 的第三方库 从工作簿中获取工作表 读取单元格数据 遍历行和列 写入 Excel 电子表格 添加和删除工作表 添加和删除行和列
https://openpyxl.readthedocs.io/en/stable/
xlrd – 用于读取旧格式的 Excel (.xls) 文件 xlwt – 用于写入旧格式的 Excel (.xls) 文件 xlwings – 用于新格式的Excel格式并具有宏功能
$ python -m pip install openpyxl
https://github.com/driscollis/python101code/tree/master/chapter38_excel
open_workbook.py
的新文件,并将以下代码添加到其中:# open_workbook.py
from openpyxl import load_workbook
def open_workbook(path):
workbook = load_workbook(filename=path)
print(f'Worksheet names: {workbook.sheetnames}')
sheet = workbook.active
print(sheet)
print(f'The title of the Worksheet is: {sheet.title}')
if __name__ == '__main__':
open_workbook('books.xlsx')
load_workbook()
函数,然后创建open_workbook()
函数,以将其导入Excel电子表格的路径中。接下来,使用load_workbook()
创建一个openpyxl.workbook.workbook.Workbook对象。该对象使您可以访问电子表格中的工作表和单元格。它确实确实具有双重工作簿的名称,那不是错字!open_workbook()
函数的其余部分演示了如何打印出电子表格中所有当前定义的工作表,如何获取当前活动的工作表以及如何打印该工作表的标题。Worksheet names: ['Sheet 1 - Books']
"Sheet 1 - Books">
The title of the Worksheet is: Sheet 1 - Books
workbook_cells.py
的新文件,并添加以下代码:# workbook_cells.py
from openpyxl import load_workbook
def get_cell_info(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
print(sheet)
print(f'The title of the Worksheet is: {sheet.title}')
print(f'The value of {sheet["A2"].value=}')
print(f'The value of {sheet["A3"].value=}')
cell = sheet['B3']
print(f'{cell.value=}')
if __name__ == '__main__':
get_cell_info('books.xlsx')
sheet ["A2"]
将为您获取第2行 A列的单元格。要获取该单元格的值,请使用value
属性。"Sheet 1 - Books">
The title of the Worksheet is: Sheet 1 - Books
The value of sheet["A2"].value='Title'
The value of sheet["A3"].value='Python 101'
cell.value='Mike Driscoll'
def get_info_by_coord(path):
workbook = load_workbook(filename=path)
sheet = workbook.active
cell = sheet['A2']
print(f'Row {cell.row}, Col {cell.column} = {cell.value}')
print(f'{cell.value=} is at {cell.coordinate=}')
if __name__ == '__main__':
get_info_by_coord('books.xlsx')
在此示例中,您将使用单元格对象的行和列属性来获取行和列信息。注意,“ A”列映射为“ 1”,“ B”映射为“ 2”,等等。如果要遍历Excel文档,则可以使用坐标属性来获取单元格名称。
运行此代码时,输出如下所示:
Row 2, Col 1 = Title
cell.value='Title' is at cell.coordinate='A2'
iterating_over_cells.py
的新文件,并向其中写入以下代码:# iterating_over_cells.py
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('books.xlsx')
'Sheet 1 - Books'.A1>
'Sheet 1 - Books'.A2>
'Sheet 1 - Books'.A3>
'Sheet 1 - Books'.A4>
'Sheet 1 - Books'.A5>
'Sheet 1 - Books'.A6>
'Sheet 1 - Books'.A7>
'Sheet 1 - Books'.A8>
'Sheet 1 - Books'.A9>
'Sheet 1 - Books'.A10> # output truncated for brevity
| | | | | | | | | |
iter_rows()
和iter_cols()
函数提供了其他遍历行和列的方法。这些方法接受下面几个参数:min_row
max_row
min_col
max_col
values_only
参数,该参数告诉OpenPyXL返回单元格而不是单元格对象的值。继续创建一个名为iterating_over_cell_values.py
的新文件,并将以下代码添加到其中:# iterating_over_cell_values.py
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('books.xlsx')
iter_rows()
迭代Excel电子表格中的行并打印出这些行的值。运行此代码时,将获得以下输出:('Books', None, None)
('Title', 'Author', 'Publisher')
('Python 101', 'Mike Driscoll', 'Mouse vs Python')
Workbook()
类创建电子表格。继续创建一个名为Writing_hello.py
的新文件,并添加以下代码:# writing_hello.py
from openpyxl import Workbook
def create_workbook(path):
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = 'Hello'
sheet['A2'] = 'from'
sheet['A3'] = 'OpenPyXL'
workbook.save(path)
if __name__ == '__main__':
create_workbook('hello.xlsx')
Workbook()
并获取当前工作表。然后将“ A”列中的前三行设置为不同的字符串。最后,调用save()
函数并向其传递新文档保存到的路径。恭喜你!您刚刚使用Python创建了一个Excel电子表格。create_sheet()
方法向Workbook()
对象添加新工作表。creating_sheets.py
的新文件,并添加以下代码:# creating_sheets.py
import openpyxl
def create_worksheets(path):
workbook = openpyxl.Workbook()
print(workbook.sheetnames)
# Add a new worksheet
workbook.create_sheet()
print(workbook.sheetnames)
# Insert a worksheet
workbook.create_sheet(index=1,
title='Second sheet')
print(workbook.sheetnames)
workbook.save(path)
if __name__ == '__main__':
create_worksheets('sheets.xlsx')
create_sheet()
将两个新的工作表添加到工作簿中。第二个示例显示了如何设置工作表的标题以及在哪个索引处插入工作表。参数index = 1
表示该工作表将在第一个现有工作表之后添加,因为它们的索引从0开始。['Sheet']
['Sheet', 'Sheet1']
['Sheet', 'Second sheet', 'Sheet1']
delete_sheets.py
文件,以了解如何使用 Python 的 del
方法删除工作表:# delete_sheets.py
import openpyxl
def create_worksheets(path):
workbook = openpyxl.Workbook()
workbook.create_sheet()
# Insert a worksheet
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('del_sheets.xlsx')
del
方法删除workbook['Second sheet']
。您可以通过查看在使用del
命令之前和之后工作表列表的打印输出来验证它是否按预期工作:['Sheet', 'Second sheet', 'Sheet1']
['Sheet', 'Sheet1']
remove()
方法。创建一个名为remove_sheets.py
的新文件,并输入以下代码以了解其工作原理:# remove_sheets.py
import openpyxl
def remove_worksheets(path):
workbook = openpyxl.Workbook()
sheet1 = workbook.create_sheet()
# Insert a worksheet
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('remove_sheets.xlsx')
sheet1
来保留对所创建的第一个工作表的引用。然后稍后在代码中将其删除。另外,您也可以使用与之前相同的语法删除该工作表,如下所示:workbook.remove(workbook['Sheet1'])
['Sheet', 'Second sheet', 'Sheet1']
['Sheet', 'Second sheet']
.insert_rows()
.delete_rows()
.insert_cols()
.delete_cols()
idx
–插入行或列的索引amount
–要添加的行数或列数
insert_demo.py
的文件,并向其中添加以下代码:# insert_demo.py
from openpyxl import Workbook
def inserting_cols_rows(path):
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = 'Hello'
sheet['A2'] = 'from'
sheet['A3'] = 'OpenPyXL'
# insert a column before A
sheet.insert_cols(idx=1)
# insert 2 rows starting on the second row
sheet.insert_rows(idx=2, amount=2)
workbook.save(path)
if __name__ == '__main__':
inserting_cols_rows('inserting.xlsx')
delete_demo.py
的新文件并添加以下代码:# delete_demo.py
from openpyxl import Workbook
def deleting_cols_rows(path):
workbook = Workbook()
sheet = workbook.active
sheet['A1'] = 'Hello'
sheet['B1'] = 'from'
sheet['C1'] = 'OpenPyXL'
sheet['A2'] = 'row 2'
sheet['A3'] = 'row 3'
sheet['A4'] = 'row 4'
# Delete column A
sheet.delete_cols(idx=1)
# delete 2 rows starting on the second row
sheet.delete_rows(idx=2, amount=2)
workbook.save(path)
if __name__ == '__main__':
deleting_cols_rows('deleting.xlsx')
delete_cols()
删除A列。它还通过delete_rows()
从第二行开始删除两行。在处理数据时,能够添加、删除列和行会非常有用。Python 处理 Excel的第三方软件包 从工作簿中获取工作表 读取单元格数据 遍历行和列 写入 Excel 电子表格 添加和删除工作表 添加、删除行和列
往期推荐
点击下方阅读原文加入社区会员
点赞鼓励一下
评论