python读取Excel文件的操作
阅读原文时间:2023年07月09日阅读:1

①通过xlutils在已有表中写数据(这种方法会改变excel的样式)

import xlrd,xlwt

from xlutils.copy import copy

将已存在的Excel表格赋值给变量

excel_file = xlrd.open_workbook("准备导入的资产.xls")

复制Excel

excel_file_copy= copy(data_w2)

根据索引获取要写入数据sheet

sheet_index = excel_file_copy.get_sheet(0)

写入数据

sheet_index.write(1, 2, "张三")

保存文件

excel_file_copy.save('准备导入的资产.xls')

②使用openpyxl(不会改变原有样式)

import openpyxl

wb = openpyxl.load_workbook(u'test.xlsx')

sheetnames =wb.sheetnames

sheet = wb[sheetnames[0]]

sheet['A1']='啦啦啦'

sheet['A2']='aaa'

sheet['C1']='就废了的减肥了设计费'

wb.save('test_result.xlsx')

③xlrd

xlrd 2.0.1版本仅支持读取.xls 格式的Excel文件。不支持读取.xlsx格式的Excel文件。如要要能读需要将版本回退,pip install xlrd==1.2.0