Today I will share with you an article on using openpyxl to operate Excel.
All kinds of data need to be imported into Excel? Multiple Excels to be merged? At present, there are many libraries for processing Excel files in Python, and openpyxl is one of the better ones in terms of function and performance. Next, I will introduce various Excel operations to you.
from openpyxl import Workbook
wb = Workbook()
> from openpyxl import load_workbook
'test.xlsx') > wb2 = load_workbook(
wb = load_workbook(filename='large_file.xlsx', read_only=True)
wb = Workbook(write_only=True)
ws = wb.active
"Mysheet") # insert at the end (default) ws1 = wb.create_sheet(
# or
"Mysheet", 0) # insert at first position ws2 = wb.create_sheet(
# or
"Mysheet", -1) # insert at the penultimate position ws3 = wb.create_sheet(
"New Title"] ws3 = wb[
>>> print(wb.sheetnames)
['Sheet2', 'New Title', 'Sheet1']
使用for循环遍历所有的工作表:
>>> for sheet in wb:
... print(sheet.title)
from tempfile import NamedTemporaryFile
from openpyxl import Workbook
wb = Workbook()
tmp: with NamedTemporaryFile() as
wb.save(tmp.name)
tmp.seek(0)
stream = tmp.read()
保存到文件:
wb = Workbook()
'balances.xlsx') wb.save(
保存为模板:
'document.xlsx') wb = load_workbook(
wb.template = True
'document_template.xltx') wb.save(
'A4'] c = ws[
'A4'] = 4 ws[
'hello, world' c.value =
You can use slices to access ranges of cells:
'A1':'C2'] cell_range = ws[
# set date using a Python datetime
'A1'] = datetime.datetime(2010, 7, 21) ws[
>>> ws['A1'].number_format
'yyyy-mm-dd h:mm:ss'
# add a simple formula
"A1"] = "=SUM(1, 1)" ws[
'A2:D2') ws.merge_cells(
'A2:D2') ws.unmerge_cells(
>>> # or equivalently
2, start_column=1, end_row=4, end_column=4) ws.merge_cells(start_row=
2, start_column=1, end_row=4, end_column=4) ws.unmerge_cells(start_row=
'C'] colC = ws[
'C:D'] col_range = ws[
0 = ws[10] row1
5:10] row_range = ws[
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):
... for cell in row:
... print(cell)
<Cell Sheet1.A1>
<Cell Sheet1.B1>
<Cell Sheet1.C1>
<Cell Sheet1.A2>
<Cell Sheet1.B2>
<Cell Sheet1.C2>
>>> for col in ws.iter_cols(min_row=1, max_col=3, max_row=2):
... for cell in col:
... print(cell)
<Cell Sheet1.A1>
<Cell Sheet1.A2>
<Cell Sheet1.B1>
<Cell Sheet1.B2>
<Cell Sheet1.C1>
<Cell Sheet1.C2>
>>> ws = wb.active
>>> ws['C9'] = 'hello world'
>>> tuple(ws.rows)
((<Cell Sheet.A1>, <Cell Sheet.B1>, <Cell Sheet.C1>),
(<Cell Sheet.A2>, <Cell Sheet.B2>, <Cell Sheet.C2>),
(<Cell Sheet.A3>, <Cell Sheet.B3>, <Cell Sheet.C3>),
(<Cell Sheet.A4>, <Cell Sheet.B4>, <Cell Sheet.C4>),
(<Cell Sheet.A5>, <Cell Sheet.B5>, <Cell Sheet.C5>),
(<Cell Sheet.A6>, <Cell Sheet.B6>, <Cell Sheet.C6>),
(<Cell Sheet.A7>, <Cell Sheet.B7>, <Cell Sheet.C7>),
(<Cell Sheet.A8>, <Cell Sheet.B8>, <Cell Sheet.C8>),
(<Cell Sheet.A9>, <Cell Sheet.B9>, <Cell Sheet.C9>))
>>> tuple(ws.columns)
((<Cell Sheet.A1>,
<Cell Sheet.A2>,
<Cell Sheet.A3>,
<Cell Sheet.A4>,
<Cell Sheet.A5>,
<Cell Sheet.A6>,
...
<Cell Sheet.B7>,
<Cell Sheet.B8>,
<Cell Sheet.B9>),
(<Cell Sheet.C1>,
<Cell Sheet.C2>,
<Cell Sheet.C3>,
<Cell Sheet.C4>,
<Cell Sheet.C5>,
<Cell Sheet.C6>,
<Cell Sheet.C7>,
<Cell Sheet.C8>,
<Cell Sheet.C9>))
>>> for row in range(1, 40):
... ws1.append(range(600))
>>> for row in range(10, 20):
... for col in range(27, 54):
... _ = ws3.cell(column=col, row=row, value="{0}".format(get_column_letter(col)))
from openpyxl.utils import get_column_letter
7) ws.insert_rows(
7] row7 = ws[
27, 54): for col in range(
... _ = ws3.cell(column=col, row=7, value="{0}".format(get_column_letter(col)))
for row in ws.values:
for value in row:
print(value)
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2, values_only=True):
... print(row)
(None, None, None)
(None, None, None)
- For cooperation, communication and reprint, please add WeChat moonhmily1 -