记录一下今天刚学的使用python自动完成excel的一些操作
创建和读取——Workbook,load_workbook
wb = openpyxl.Workbook()
sheet = wb.active
wb = openpyxl.load_workbook(excel_path)
sheet = wb["sheet1"]
cla_range = sheet["A:B"]
for row in sheet.iter_rows(1, 3, 2,2):
for cell in row:
print(cell)
from openpyxl.utils import get_column_letter, column_index_from_string
print(get_column_letter(2),column_index_from_string("A"))
-->输出为 B,1
写入excel
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = "YM"
sheet.cell(1,2).value = "sfasfsaf"
wb.create_sheet(index=3, title="4")
print(wb.sheetnames)
for row in range(1,40):
sheet.append(range(20))
字典的相关操作setdefault
my_dict = {}
my_dict.setdefault("ym", {"track": 0, "pop": 0})