openpyxl module Introduction
openpyxl module is a Python library to read and write Excel 2010 document, if you want to deal with earlier Excel file format, the need to use additional libraries, openpyxl is a more comprehensive tool to read and modify Excel documents simultaneously. Many other basic items related to Excel only supports a feature to read or write Excel.
Installation openpyxl module
openpyxl is an open source program, as used herein, the following command module mounted openpyxl
pip3 install openpyxl
A. Made Excel file
1. made empty excel
import openpyxl
if __name__ == '__main__': #作成一个新的空的Excel wb = openpyxl.Workbook() # 保存 wb.save('example.xlsx')
2. Change the name of the default page of sheet
import openpyxl
if __name__ == '__main__': wb = openpyxl.Workbook #当前打开的sheet页 wb.active ws = wb.active # 更改默认名称Sheet` ws.title = "WorkSheetTitle" # 保存 wb.save('example.xlsx')
3. creating Excel sheet containing multiple pages
import openpyxl
if __name__ == '__main__': wb = openpyxl.Workbook() #当前打开的sheet页 wb.active ws = wb.active # #更改默认名称Sheet` ws.title = "WorkSheetTitle" # 定义第二个sheet页 ws2 = wb.create_sheet("NewWorkSheet2") # 定义第三个sheet页 # `0` 的设定 会将该sheet页 置于wb最前面 ws3 = wb.create_sheet("NewWorkSheet3", 0) # 保存 wb.save('example.xlsx')
4.sheet WS-page color change of the tab
import openpyxl
if __name__ == '__main__': wb = openpyxl.Workbook() ws = wb.active # 更改默认名称Sheet` ws.title = "WorkSheetTitle" # WS的tab颜色设定 ws.sheet_properties.tabColor = "1072BA" # 保存 wb.save('example.xlsx')
II. Cell Assignment
Specify a coordinate
if __name__ == '__main__':
wb = openpyxl.Workbook()
ws = wb.active
# 更改默认名称Sheet`
ws.title = "WorkSheetTitle"
# 给单元格赋值
ws["A1"] = "HOGE"
ws["B1"] = "FUGA"
# 保存
wb.save('example.xlsx')
2. Specify the ranks
if __name__ == '__main__':
wb = openpyxl.Workbook()
ws = wb.active
# 更改默认名称Sheet`
ws.title = "WorkSheetTitle"
# 指定行列给单元格赋值
ws.cell(row=4, column=2, value=10)
# 保存
wb.save('example.xlsx')
3. Specify range
import openpyxl
if __name__ == '__main__':
wb = openpyxl.Workbook() ws = wb.active # 更改默认名称Sheet` ws.title = "worksheettitle" # 指定行列给单元格赋值 v = 0 for i in range(1,10): for n in range(1,10): ws.cell(row=i, column=n, value=v) v += 1 # 保存 wb.save('example.xlsx')
4. The next output row
import openpyxl
# column名
column_title = ["FirstName", "LastName"] if __name__ == '__main__': """ CELL放入值 """ wb = openpyxl.Workbook() ws = wb.active # 更改默认名称Sheet` ws.title = "worksheettitle" # column名和値顺序放入单元格中 rows = [ column_title, ["Tarou", "Tanaka"], ["Tarou", "Suzuki"], ["Tarou", "Uchiayama"], ] for row in rows: ws.append(row) # 保存 wb.save('example.xlsx')
5. Wrap the cell
import openpyxl
if __name__ == '__main__': """ 单元格内换行 """ wb = openpyxl.Workbook() ws = wb.active # 更改默认名称Sheet` ws.title = "WorkSheetTitle" # 单元格内换行 ws['A1'] = "A\nB\nC" ws['A1'].alignment = openpyxl.styles.Alignment(wrapText=True) # 保存 wb.save('example.xlsx')
III. Set the cell style
1.style presentation
https://openpyxl.readthedocs.io/en/default/styles.html#cell-styles-and-named-styles
2. Set the font font
import openpyxl
if __name__ == '__main__': """ 设置字体font """ wb = openpyxl.Workbook() ws = wb.active # 更改默认名称Sheet` ws.title = "worksheettitle" # 设置font font = openpyxl.styles.Font( name = "宋体", size = 15, ) a1 = ws["A1"] a1.font = font a1.value = "TEST" # 保存 wb.save('example.xlsx')
3. The cell border border
import openpyxl
from openpyxl.styles import Border, Side if __name__ == '__main__': """ 设置单元格style """ wb = openpyxl.Workbook() ws = wb.active # 更改默认名称Sheet` ws.title = "worksheettitle" # 设置单元格border的style border = Border( left=Side( border_style="thin", color="FF0000" ), right=Side( border_style="thin", color="FF0000" ), top=Side( border_style="thin", color="FF0000" ), bottom=Side( border_style="thin", color="FF0000" ) ) b2 = ws["B2"] b2.border = border b2.value = "TEST" # 保存 wb.save('example.xlsx')
4. Merge Cells
import openpyxl
if __name__ == '__main__': wb = openpyxl.Workbook() ws = wb.active ws.title = "worksheettitle" # 合并单元格 ws.merge_cells("A1:E1") ws["A1"] = "HOGE" # 保存 wb.save('example.xlsx')
The cell fill color
import openpyxl
from openpyxl.styles import PatternFill
if __name__ == '__main__': wb = openpyxl.Workbook() ws = wb.active ws.title = "worksheettitle" # 单元格填充颜色 fill = PatternFill(fill_type='solid', fgColor='FFFF0000') b2 = ws["B2"] b2.fill = fill b2.value = "TEST" # 保存 wb.save('example.xlsx')
Four .hyperlink Hyperlinks
import openpyxl
if __name__ == '__main__': wb = openpyxl.Workbook() ws = wb.active ws.title = "worksheettitle" # 作成第二个sheet页 名称胃example ws2 = wb.create_sheet("example") # 设置超链接 到“example”sheet页 鼠标定格在A5单元格 ws["A1"] = "Link" ws["A1"].hyperlink = "example.xlsx#example!A5" # 保存 wb.save('example.xlsx')
V. read Excel documents
from openpyxl import Workbook, load_workbook
wb = load_workbook('./example.xlsx') ws = wb.active for row in ws: for cell in row: print(cell)