1.開いてシートを取得する
まず、2つのライブラリをインストールする必要があります。
- 読み取り用のxlrd
- xlwtは書き込みに使用されます
Excelファイルを開きます。
- xlrd.open_workbook( "xxx.xls")
シートを入手:
import xlrd
workbook = xlrd.open_workbook('成绩表.xlsx')
# 获取sheet名字
print(workbook.sheet_names())
sheet = workbook.sheet_by_index(0)
print(sheet.name)
sheet = workbook.sheet_by_name('1班')
print(sheet.name)
sheets = workbook.sheets()
for sheet in sheets:
print(sheet.name)
# 获取指定sheet的行列列数
sheet = workbook.sheet_by_index(0)
print({"rows": sheet.nrows, "cols": sheet.ncols})
2.セルとその属性を取得する
# 获取指定sheet的行列列数
sheet = workbook.sheet_by_index(0)
print({"rows": sheet.nrows, "cols": sheet.ncols})
sheet = workbook.sheet_by_index(0)
cell = sheet.cell(0, 0)
print(cell.value)
cell_value = sheet.cell_value(0, 1)
print(cell_value)
cells = sheet.row_slice(1, 1, 4)
for cell in cells:
print(cell.value)
cells = sheet.col_slice(0, 1, sheet.nrows)
for cell in cells:
print(cell.value)
cell_values = sheet.col_values(1, 1, sheet.nrows)
print(cell_values)
cell_values = sheet.row_values(1, 1, sheet.ncols)
print(cell_values)
print(sum(cell_values), sum(cell_values)/3.0)
3.セルの共通データ型
# cell类型
sheet = workbook.sheet_by_index(0)
cell = sheet.cell(0, 0)
print(cell.ctype)
print(xlrd.XL_CELL_TEXT)
cell = sheet.cell(1, 1)
print(cell.ctype)
print(xlrd.XL_CELL_NUMBER)
cell = sheet.cell(20, 0)
print(cell.ctype)
print(xlrd.XL_CELL_DATE)
cell = sheet.cell(19, 0)
print(cell.ctype)
print(xlrd.XL_CELL_BOOLEAN)
cell = sheet.cell(0, 0)
print(cell.ctype)
print(xlrd.XL_CELL_EMPTY)
4. Excelの書き込み手順
import xlwt
import random
workbook = xlwt.Workbook()
sheet = workbook.add_sheet('sheet1')
headers = ['姓名', '语文', '英语', '数学']
for index, header in enumerate(headers):
sheet.write(0, index, header)
names = ['张三', '李四', '王五']
scores = []
for index, name in enumerate(names):
sheet.write(index+1, 0, name)
for row in range(1, 4):
for col in range(1, 4):
sheet.write(row, col, random.randint(90, 100))
workbook.save('成绩表2.xls')
5. Excelファイルを編集する
- 最初に元のExcelファイルを読み取ります
- 上記を読んでセルを変更し、sheet.put_cell(row、col、ctype、value、None)を使用して達成します
- Excelファイルを再作成し、以前に読み取ったデータを新しいExcelに書き込む
import xlwt, xlrd
rwb = xlrd.open_workbook('成绩表.xlsx')
rsheet = rwb.sheet_by_index(0)
rsheet.put_cell(0, 4, xlrd.XL_CELL_TEXT, '总分', None)
nrows = rsheet.nrows
for row in range(1, nrows):
grades = rsheet.row_values(row, 1, 4)
print(grades)
print(type(grades[0]))
total = sum(grades)
rsheet.put_cell(row, 4, xlrd.XL_CELL_NUMBER, total, None)
nrows = rsheet.nrows
ncols = rsheet.ncols
for col in range(1, 5):
grades = rsheet.col_values(col, 1, nrows)
avg = sum(grades) / len(grades)
rsheet.put_cell(nrows, col, xlrd.XL_CELL_NUMBER, avg, None)
wwb = xlwt.Workbook()
wsheet = wwb.add_sheet('sheet1')
for row in range(0, rsheet.nrows):
for col in range(0, rsheet.ncols):
wsheet.write(row, col, rsheet.cell_value(row, col))
wwb.save('abc.xls')