本文共 2316 字,大约阅读时间需要 7 分钟。
? PyCharm ??? openpyxl ???? Excel ???????????????????????????????
????? openpyxl ????????????????????????
pip install openpyxl
????????????????? pywin32 ???? openpyxl ? Windows ??????????
pip install pywin32
?? openpyxl ?? Excel ????????
from openpyxl import load_workbook# ?? Excel ??fp = load_workbook("test.xlsx") ???????????
# ??????????????sheetnames = fp.sheetnames# ????????sheetname = sheetnames[0]
?????????????
# ?? A1 ???print("A1", sheet["A1"].value)# ?? 1 ???????print("1?", sheet["1"])# ?? C4 ?????print("C4", sheet["C4"].value) ????????????
print("???", sheet.max_row)print("???", sheet.max_column) ???????????????
# ?? A1 ?????print("A1", sheet.cell(row=1, column=1).value) ????????????????????????? save()???????
fp.save("test.xlsx") ???????????????????????????????
# ??????print(os.path.exists("test.xlsx")) ????????????? get_sheet_names() ??????????
sheetnames = fp.sheetnamessheetname = sheetnames[0]
?? create_sheet() ?????????
from openpyxl import workbookwb = workbook.Workbook()sheet = wb.activesheet.title = "new sheet"wb.save("test.xlsx") ?? delete_sheet() ????????
sheet = wb["Sheet1"]wb.delete_sheet(sheet)
?????????
sheet["C3"].value = "Hello world!"
???????
sheet["E1"].value = "=SUM(A:A)"
??????????
wb.save("test.xlsx") ???????????????????????????
os.chmod("test.xlsx", 0o644) ?? max_row() ? min_row() ?????
print("???", sheet.max_row)print("???", sheet.min_row) ??????????
for i in sheet["C"]: print(i.value, end=" ")
?????????
print(sheet["B2"].value)
????????
from openpyxl import load_workbook# ?? Excel ??fp = load_workbook("test.xlsx")# ????????sheet = fp["Sheet1"]# ?? A ???????for cell in sheet["A"]: print(cell.value)# ?? C ???????for cell in sheet["C"]: print(cell.value)# ?? 2 ???????for cell in sheet["2"]: print(cell.value)# ????fp.save("test.xlsx") zipfile.BadZipFile ??????????????DeprecationWarning: Call to deprecated function get_sheet_by_name???? wb[sheetname] ?? get_sheet_by_name()???????????? PyCharm ?????? openpyxl ???? Excel ???
转载地址:http://ccpfk.baihongyu.com/