import openpyxl
workbook = openpyxl.load_workbook("test.xlsx")
# 输出工作簿中所有表单名字
print(workbook.sheetnames)
# 遍历所有表单并输出其名字
for sheet in workbook:
print(sheet.title)
newSheet = workbook.create_sheet("newSheetName")
# 根据表单名获取表单
sheet3 = workbook.get_sheet_by_name("Sheet3")
sheet4 = workbook["newSheetName"]
# 获取当前活跃的表单
worksheet = workbook.active
# 获取当前表单数据行数
row_count = worksheet.max_row
# 获取当前表单数据列数
row_count = worksheet.max_column
selectcell = worksheet["A1"]
selectcell = worksheet.cell(row=1, column=2) # 行列号从1开始
print(selectcell.row, selectcell.column)
print(selectcell.coordinate)
print(selectcell.value)
selectcol = worksheet["C"]
selectcell = selectcol["2"]
selectcols = worksheet["B:C"]
allcol = worksheet.cols
selectrow = worksheet["2"]
selectcell = selectcol["C"]
selectcols = worksheet["1:4"]
allrow = worksheet.rows
cell_range = worksheet["A1:C3"]
for row_range in cell_range: # 先行再列
for cell in row_range:
print(cell.value)
from openpyxl.utils import get_column_letter, column_index_from_string
print(get_column_letter(2))) # 2 => B
print(column_index_from_string("C")) # C => 3
workbook = openpyxl.Workbook()
workbook.create_sheet(index=1, titl)
workbook.remove_sheet(workbook.get_sheet_by_name("Sheet3"))
sheet = workbook.active
sheet.title = "设置的表单名称"
sheet["A1"] = "New Value"
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
workbook = openpyxl.Workbook()
ws1 = workbook.create_sheet("第一页")
for row in range(40):
ws1.append(range(17))
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
workbook = openpyxl.Workbook()
ws2 = workbook.create_sheet("第二页")
rows = [
["Number", "Batch1", "Batch2"],
[2,40,30],[3,50,25],
[4,30,30],[5,60,10]
]
for row in rows:
ws2.append(row)
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
workbook = openpyxl.Workbook()
ws3 = workbook.create_sheet("第三页")
for row in range(5,30):
for col in range(15,24):
ws3.cell(column=col, row=row, value=123)
workbook.save("created.xlsx")
想进一步了解编程开发相关知识,与我一同成长进步,请关注我的公众号“松果仓库”,共同分享宅&程序员的各类资源,谢谢!!!
审核编辑 黄昊宇
全部0条评论
快来发表一下你的评论吧 !