当前位置:   article > 正文

xlrd方法excel_xlrd2

xlrd2

获取表及表名

import xlrd2
path = r'D:\pycharm\实验\表.xlsx'
data = xlrd2.open_workbook(path)
sheets = data.sheet_names()
for sheet_name in sheets:
    print("表格的名称是:", sheet_name)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
import xlrd2
path = r'D:\pycharm\实验\表.xlsx'
data = xlrd2.open_workbook(path)
sheets = data.sheet_names()
for sheet_name in sheets:
    print("表格的名称是:", sheet_name)
# 打开目标xlsx中的目标sheet
sheetName = 'Sheet1'
data = xlrd2.open_workbook(path)
table = data.sheet_by_name(sheetName)
# rx、cx分别是行列序数
cellInfo = table.cell_value(2, 3)
# 行数
rowAmount = table.nrows
# 列数
colAmount = table.ncols
# 显示第n(从0开始)行所有格中的内容
for colIndex in range(colAmount):
    print(table.cell_value(0, colIndex), end=' ')
# 显示第n列中所有格中的内容
for rowIndex in range(rowAmount):
    print(table.cell_value(rowIndex, 2))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
import xlrd2
import xlwt
# 获取excel
path = r'D:\pycharm\实验\表.xlsx'
data = xlrd2.open_workbook(path)
# 建立新表
new_workbook = xlwt.Workbook()
worksheet = new_workbook.add_sheet('Mytest')
'''
# 获取excel名称
sheets = data.sheet_names()
for sheet_name in sheets:
    print("表格的名称是:", sheet_name)
'''
# 打开目标xlsx中的目标sheet
sheetName = 'Sheet1'
table = data.sheet_by_name(sheetName)
# 行数
rowAmount = table.nrows
# 列数
colAmount = table.ncols
'''
# rx、cx分别是行列序数
cellInfo = table.cell_value(2, 2)
# 显示行所有格中的内容
for i in range(0, rowAmount):
    for colIndex in range(colAmount):
        print(table.cell_value(i, colIndex), end=' ')
    print('\n')
# 显示列中所有格中的内容
for i in range(0, colAmount):
    for rowIndex in range(rowAmount):
        print(table.cell_value(rowIndex, i), end=' ')
    print('\n')
'''
print('请输入学号:')
n = int(input())
# 判断指定格
for i in range(colAmount):
    if table.cell_value(0, i) == '序号':
        for rowIndex in range(rowAmount-1):
            print(int(table.cell_value(rowIndex+1, i)))
for i in range(colAmount):
    worksheet.write(0, i, table.cell_value(0, i))
new_workbook.save('D:/pycharm/实验/学生成绩统计 实验.xlsx')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/910749
推荐阅读
相关标签
  

闽ICP备14008679号