python learning to read -excel


# Third-party libraries openpyxl

# install 安装
# pip install openpyxl

Third-party libraries #
# Excel operation process:
# open excel, into the workbook Workbook
# selected form Sheet
# cells in the Cell
# write operation

# Test data already exist. Form at least exist.

from openpyxl import load_workbook
wb = load_workbook(r'D:\Pychram-Workspace\python17\class_20190507\datas.xlsx')
# wb.read_only
# from openpyxl.workbook import Workbook

# 表单 workSheet
sh = wb["case_datas"]

Get a cell # - 1 from the start value. # Reading cell value
print (sh.cell (1,1) .value)

# # Write cell values
# sh.cell (6,1) .value = "Ergou"

# # Write data stored - entire workbook
# wb.save (r'D: \ Pychram- Workspace \ python17 \ class_20190507 \ datas.xlsx ')


# 总行号
rows = sh.max_row
print(rows)
colums = sh.max_column
print(colums)

# Traverse line number
for row in range (2, rows + 1): # line number
print ( "line a few:", Row)
row_datas = {}
for COL in Range (. 1, colums +. 1): # column number
# Print (sh.cell (row, COL) .Value)
# The first line does not move. The first line of all the columns are key key whose corresponding value is?
row_datas [sh.cell (. 1, COL) .Value] = sh.cell (Row, COL) .Value
Print ( "Bank data:", row_datas)


# Name: Little Jane Age: 20 Class: PY17
# The first line does not move. The first line of all the columns are key key whose corresponding value is?

# Packaged as a class. exel style content. Read (a row, all of the data), a write operation, the save operation
# test their class, whether the function bug.

# pip install pandas

# Today summary
# reflecting the hasattr getattr setattr delattr
# Exel data manipulation - openpyxl
# Workbook, Sheet, Cell
# loading a workbook WB = load_workbook (excel_filepath) R & lt
# pick form sh = wb [ "Form Name"]
# picked cell sh .cell (row, column) # Object
# reading the cell value = sh.cell (row, column) .value # 1 starts from index
# modify / add values sh.cell (Row, column) .Value = NEW_VALUE
# save wb.save (excel_filepath) # save as is determined by the path, or the original file.
# Headquarters No. sh.max_row
# Total column number sh.max_column

# Read all the data for for

 

Guess you like

Origin www.cnblogs.com/qsmyjz/p/11261263.html