Python 处理Excel

1. 安装openpyxl

 

pip install openpyxl

 

 

2. 读取Excel和写入Excel

 

 

import openpyxl

wb = openpyxl.load_workbook('example.xlsx')

sheet = wb.get_sheet_by_name('Sheet1')

for i in range(1, 8, 2):
    print(i, sheet.cell(row=i, column=2).value)

sheet['A9'] = 200
sheet['C8'] = '=SUM(C1:C7)'

sheet.cell(row=9, column=2).value = 122
wb.save('example.xlsx')

可以通过row和colum来确定某一个格子,也可以用A9这样的。内容可以直接写公式

 

    try:
        wb.remove_sheet(wb['Sheet1'])
    except Exception as e:
        pass
    try:
        sheet = wb[target_sheet_name]
    except Exception as e:
        sheet = wb.create_sheet()

    sheet.title = target_sheet_name
    sheet.column_dimensions['A'].width = 30

 

http://www.waitingfy.com/archives/1824

1824

Leave a Reply

Name and Email Address are required fields.
Your email will not be published or shared with third parties.