赞
踩
https://openpyxl.readthedocs.io/en/stable/index.html
https://openpyxl-chinese-docs.readthedocs.io/zh_CN/latest/api/openpyxl.html
openpyxl.workbook.workbook.Workbook
opnepyxl.worksheet.worksheet.Worksheet
openpyxl.cell.cell.Cell
#加载workbook对象
wb=openpyxl.load_Workbook(Filename)
#创建workbook对象
wb=openpyxl.Workbook()
#根据sheetname加载worksheet对象
sh=workbook[sheetname]
#创建worksheet对象
sh=workbook.creat_sheet(sheetname)
#根据Cell的坐标加载某个单元格
cell=worksheet[cellcoordinate]
#根据列号获取某一列的单元格
cellrange=worksheet[‘A’]
#根据列号获取某几列的单元格
cellrange=worksheet[‘A:C’]
#根据行号获取某一行的单元格
cellrange=worksheet[3]
#根据列号获取某几列的单元格
cellrange=worksheet[3:5]
#根据最小边界范围加载一个区域的单元格
cellrange=worksheet[“A1:C2”]
workbook.sheetnames 返回workbook中所有sheet的sheename
workbook.active 返回workbook中激活的workbsheet对象
workbook.worksheets 返回workbook中所有的worksheet对象
workbook.chartsheets 返回workbook中所有的charsheet对象
workbook.get_sheet_names() 返回workbook中所有sheet的sheename 已弃用
workbook.get_active_sheet() 返回workbook中激活的workbsheet对象 已弃用
workbook.get_index(n) 返回workbook中索引为n的workbsheet对象 已弃用
workbook.save(filename) Save the current workbook under the given filename.
workbook.close() Close workbook file if open. Only affects read-only and write-only modes.
workbook.index(n) 返回workbook中索引为n的workbsheet对象
workbook.get_sheet_by_name(sheetname) 返回workbook中名称为shetname的workbsheet对象
workbook.creat_sheet(title,index) 创建一个名称为tilte的worksheet
workbook.remove_sheet(worksheet) 从workbook中删除这个worksheet
workbook.move_sheet(sheet.offset) 移动一个worksheet
workbook.copy_worksheet(from worksheet) 复制一个本workbook中的worksheet
workbook.remove(worksheet) 从workbook中删除这个worksheet[同remove_sheet]
Property | Discription |
---|---|
worksheet.value | Produces all cell values in the worksheet, by row |
worksheet.rows | Produces all cells in the worksheet, by row |
worksheet.columns | Produces all cells in the worksheet, by column |
worksheet.dimensions | Returns the result of the minimum bounding range for all cells containing data |
worksheet.max_row | The maximum row index containing data (1-based) |
worksheet.max_column | The maximum column index containing data (1-based) |
worksheet.min_row | The minimum row index containing data (1-based) |
worksheet.min_column | The minimum column index containing data (1-based) |
function | Discription |
---|---|
cell=worksheet.cell(row, column) | Returns a cell object based on the given coordinates. |
insert_cols(idx, amount=1) | Insert column or columns before col=idx |
insert_rows(idx, amount=1) | Insert row or rows before row=idx |
delete_cols(idx, amount=1) | Delete column or columns from col=idx |
delete_rows(idx, amount=1) | Delete row or rows from row=idx |
iter_cols(min_col=None, max_col=None, min_row=None, max_row=None, values_only=False) | Produces cells from the worksheet, by column. Specify the iteration range using indices of rows and columns. |
iter_rows(min_row=None, max_row=None, min_col=None, max_col=None, values_only=False) | Produces cells from the worksheet, by row. Specify the iteration range using indices of rows and columns. |
Porperty | Discription |
---|---|
cell.value | Get or set the value held in the cell. |
cell.row | Row number of this cell (1-based) |
cell.column | Column number of this cell (1-based) |
cell.coordinate | This cell’s coordinate (ex. ‘A5’) |
cell.comment | Returns the comment associated with this cell |
cell.data_type | Returns the data type associated with this cell |
import openpyxl
#新建workbook
wb = openpyxl.Workbook()
#新建worksheet
sh = wb.create_sheet("Bank Data",index=0)
#将数据写入worksheet
sh['A1'] = "银行"
sh.append(['中国银行'])
sh.cell(3,1,'中国农业银行')
sh.cell(4,1,'中国建设银行')
#保存workbook
wb.save("Bank Deposit Interest Rate Table.xlsx ")
#关闭workbook
wb.save()
import openpyxl #加载名字为"Bank Deposit Interest Rate Table.xlsx "的Excel wb = openpyxl.load_workbook('Bank Deposit Interest Rate Table.xlsx') #加载名字为”Bank Data“的sheet sh = wb['Bank Data'] #更改sheet中的数据 sh['B1']='利率' sh['B2']='1.5%' sh['B3']='1.4%' sh['B4']='1.5%' #输出worksheet的各种属性文件 print(sh.value) print(sh.rows) print(sh.columns) print(sh.dimensions) #输出各属性对象 for row in sh.values: print(row) for row in sh.rows: print(row) for column in sh.columns: print(column) #根据sheet属性,遍历sheet for row in sh.values: for value in row: print(value) for row in sh.rows: for cell in row: print(cell.value) for column in sh.columns: for cell in column: print(cell.value)
import openpyxl """load workbook""" wb = openpyxl.load_workbook('Bank Deposit Interest Rate Table.xlsx') sh = wb['Bank Data'] sh["B1"]="利率" sh["B2"]="1.5%" sh["B3"]="1.4%" sh["B4"]="1.5%" cellA = sh['A1'] cellB = sh['B1'] FontA = openpyxl.styles.Font(name='Microsoft YaHei', color='111111',size =20,bold=True, italic=True,underline='single',strikethrough=True,outline=True,scheme="major",shadow=True,) FontB = openpyxl.styles.Font(name="微软雅黑",size =15,bold=True, italic=True,underline='single',strikethrough=True,outline=False,scheme="minor",shadow=False,) cellA.font=FontA cellB.font=FontB wb.save('Bank Deposit Interest Rate Table.xlsx') wb.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。