Preparation
- Install related modules
pip install openpyxl lxml pillow
- basic definition
Workbook: A spreadsheet file is a workbook
Active table: The table currently viewed by the user or the last table viewed by Excel when closing Excel
sheet table
Cell
Excel data reading operation
- Open the workbook and create an object:
wb = openpyxl.load_workbook('E:\PyCharm\pythonProject\test\venv\test.xlsx')
- Get the representation of all sheets in the workbook:
wb.sheetnames
- Get the specified sheet object:
sheet = wb['upgrade content']
- Get the active table object:
sheet_ac = wb.active
- Get the cell object:
cell = sheet['A4']
- Get cell contents:
print(cell.value)
- Get coordinates:
print(cell.row) #Get the row coordinates
print(cell.column) #Get the column coordinates
print(cell.coordinate) # Get the entire coordinates
- Locate cell objects by row and column index
print(sheet.cell(row=4,column=3).value)
- Facilitates the selected area,The selected area is returned as a tuple
for cell_row in sheet['C2':'D11']:
for cell in cell_row:
print(cell.value,cell.coordinate)
- Get the value of a cell in a specific row and column: use the rows and columns properties of the worksheet object
( returns a generator object, which can be formatted into a list using list() and traverses the specified rows or columns)
print(list(sheet.rows))
print(list(sheet.columns))
for cell_row in list(sheet.rows)[0]:
print(cell_row.value)
for cell_row in list(sheet.columns)[0]:
print(cell_row.value)
- Get the maximum number of rows and columns in a worksheet
print(sheet.max_row,sheet.max_column)
Write to Excel document
- Create a new workbook object
wb = openpyxl.Workbook()
- Rename the worksheet
Sheet.title = "Record Sheet"
- Save workbook
wb.save('C:\\Users\\Admin\\Desktop\\Date.xlsx')
- Create new worksheet
wb.create_sheet(title='Record Sheet 2')
wb.create_sheet(index=1,title='Record Sheet 3' ;)
#index=1: Indicates the position where the newly created worksheet is placed
- delete worksheet
del wb['Record Table 3']
- Write to cell
sheet['A1'] = 'hello'
sheet.cell(row=1, column=2).value = 100
Cell styling
- Set font
Import font tool class:
from openpyxl.styles import Font
Set font:
sheet['A3'].font = Font(name = '楷体')
Set color:
sheet['A3'].font = Font(color = '8470FF')
other settings:
italic = True #Set italic
size = XXX #Set font size
underline='sigle' #Set single underline
b = True #Bold
...
- Set background fill color
Import background fill color tool class
from openpyxl.styles import PatternFill
Fill background color
sheet['A3'].fill = PatternFill(patternType='solid',fgColor='8470FF')
1
- Set borders
Import the border tool class Side as the side and Border as the box
from openpyxl.styles import Side,Border
Customized borders
#Set two edge objects, equivalent to two templates
S1 = Side(style='thin',color='8470FF')
S2 = Side(style='double',color='ff0000')
#Define the upper border of cell A3 to be the S1 template style
sheet['A3'].border = Border(top=S1)
# Define the 4 border styles of cell A4 respectively
- Cell alignment
Import Alignment Tool Class
from openpyxl.styles import Alignment
Define alignment
#horizontal is the horizontal alignment, left is the left alignment
sheet['B2'].alignment = Alignment(horizontal='left')
Data filtering and sorting
- Define filters
#auto_filter: Define a filter object, ref is the selection range
sheet.auto_filter.ref = 'C1:C280'
- Add filter conditions to filter
(There will be a bug. After execution, it has actually taken effect but the excel table is not displayed. You need to click on the filter to confirm)
#2 is to filter the content of the third column, ['coliforms'] is the filtering condition
sheet.auto_filter.add_filter_column(2,['coliforms'])
- Data sorting
#You also need to define a filter first
sheet.auto_filter.ref = 'C1:C280'
#Parameter 1: Sorting column, Parameter 2: Ascending and descending order
sheet.auto_filter.add_sort_condition(ref='C3:C20',descending=True)
other settings
- Formula operations
Add formula
sheet['A3'] = '=SUM(A1:A2)'
When you open a table with formulas and want to read data instead of formulas, you need read-only mode. There is a bug: if it does not take effect, you need to open excel and save it again.
xl = openpyxl.load_workbook('C:\\Users\\Administrator\\Desktop\\10.19.xlsx',read_only=True)
- Set rows and columns
Set row height
#sheet.row_dimensions: Get rows and return them as a dictionary containing row objects, one key per row and the corresponding object value. Select the object corresponding to key 2, that is, set the height of the second row to 50
sheet.row_dimensions[2].height = 50
Set column width
sheet.column_dimensions['A'].width = 80
- Split merged cells
Merge Cells
sheet.merge_cells('A1:D7')
Split cells
sheet.unmerge_cells('A1:D7')
- Freeze and Thaw Windows
Freeze
sheet.freeze_panes = 'A2'
Thaw
sheet.freeze_panes = None
Draw charts
#'G1': The position added for the icon
Comprehensive example
As shown in the table below, the company requires that the number of testing items and the abbreviations of testing methods in enterprises and governments be counted on two sheet pages every day and output to a new excel. The number suffixed by the testing items is the number of testing (for example, there are two E. coli* 5, two E. coli, the total number of times is 2*5+2)
Code:
from openpyxl.styles import Alignment
import openpyxl
xl = openpyxl.load_workbook('C:\\Users\\Admin\\Desktop\\10.19.xlsx')
ls = xl.sheetnames
sheet1 = xl[ls[0]]
sheet2 = xl[ls[1]]
countDate = {}
for row in range(2,sheet1.max_row + 1):
state = sheet1['C' + str(row)].value
name = sheet1['D' + str(row)].value
if state != None:
countDate.setdefault(state,{'counts':0,'检测方法简称':name})
countDate[state]['counts'] += 1
print(countDate)
for row in range(2,sheet2.max_row + 1):
state = sheet2['C' + str(row)].value
name = sheet2['D' + str(row)].value
if state != None:
countDate.setdefault(state,{'counts':0,'检测方法简称':name})
countDate[state]['counts'] += 1
countNum = {}
for key,value in countDate.items():
numm = key.find('*')
if numm < 0:
if countNum.get(key) == None:
countNum.setdefault(key,value)
else:
countNum[key]['counts'] += int(countDate[key]['counts'])
else:
names = key[0:numm]
nums =int(key[numm+1:]) * int(countDate[key]['counts'])
namess = countDate[key]['检测方法简称']
if countNum.get(names) == None:
countNum.setdefault(names,{'counts':nums,'检测方法简称':namess})
else:
countNum[names]['counts'] += nums
print("++++++++++++++++++++++++++++++++++")
print(countNum)
wb = openpyxl.Workbook()
sheet = wb.active
sheet.title = '统计数据'
sheet.column_dimensions['A'].width=25
sheet.column_dimensions['A'].horizontal='left'
sheet.column_dimensions['C'].width=100
no1 = 0
for key,value in countNum.items():
no1 += 1
sheet.cell(row = no1 , column=1).value = key
sheet.cell(row=no1, column=2).value = countNum[key]['counts']
sheet.cell(row=no1, column=2).alignment = Alignment(horizontal='left')
sheet.cell(row=no1, column=3).value = countNum[key]['检测方法简称']
wb.save('C:\\Users\\Admin\\Desktop\\Date.xlsx')