Python và Excel

Excel là một ứng dụng bảng tính phổ biến và mạnh mẽ dành cho Windows. Mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6 cho phép các chương trình Python của bạn đọc và sửa đổi các tệp bảng tính Excel. Ví dụ: bạn có thể có nhiệm vụ nhàm chán là sao chép một số dữ liệu nhất định từ một bảng tính và dán nó vào một bảng tính khác. Hoặc bạn có thể phải xem qua hàng nghìn hàng và chỉ chọn ra một số hàng để thực hiện các chỉnh sửa nhỏ dựa trên một số tiêu chí. Hoặc bạn có thể phải xem qua hàng trăm bảng tính ngân sách của bộ phận, tìm kiếm bất kỳ bảng tính nào có màu đỏ. Đây chính xác là loại nhiệm vụ bảng tính nhàm chán, không cần suy nghĩ mà Python có thể làm cho bạn

Mặc dù Excel là phần mềm độc quyền của Microsoft nhưng vẫn có những lựa chọn thay thế miễn phí chạy trên Windows, OS X và Linux. Cả LibreOffice Calc và OpenOffice Calc đều hoạt động với của Excel. xlsx định dạng tệp cho bảng tính, có nghĩa là mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6 cũng có thể hoạt động trên bảng tính từ các ứng dụng này. Bạn có thể tải xuống phần mềm từ https. //www. thư viện. org/http. //www. mở văn phòng. org/ , tương ứng. Ngay cả khi bạn đã cài đặt Excel trên máy tính của mình, bạn vẫn có thể thấy các chương trình này dễ sử dụng hơn. Tuy nhiên, các ảnh chụp màn hình trong chương này đều từ Excel 2010 trên Windows 7.

tài liệu excel

Đầu tiên, hãy xem qua một số định nghĩa cơ bản. Tài liệu bảng tính Excel được gọi là sổ làm việc . Một sổ làm việc được lưu trong một tệp có . xlsx tiện ích mở rộng. Mỗi sổ làm việc có thể chứa nhiều trang tính [còn được gọi là trang tính ]. Trang tính mà người dùng hiện đang xem [hoặc được xem lần cuối trước khi đóng Excel] được gọi là trang tính đang hoạt động .

Mỗi trang tính có cột [được đánh địa chỉ bằng các chữ cái bắt đầu từ A] and rows [addressed by numbers starting at 1]. A box at a particular column and row is called a ô . Mỗi ô có thể chứa một số hoặc giá trị văn bản. Lưới các ô có dữ liệu tạo thành một trang tính.

Cài đặt Mô-đun openpyxl

Python không đi kèm với OpenPyXL, vì vậy bạn sẽ phải cài đặt nó. Làm theo hướng dẫn cài đặt mô-đun của bên thứ ba trong Phụ lục A; . Để kiểm tra xem nó đã được cài đặt đúng chưa, hãy nhập thông tin sau vào trình bao tương tác

>>> import openpyxl

Nếu mô-đun được cài đặt chính xác, điều này sẽ không tạo ra thông báo lỗi. Hãy nhớ nhập mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6 trước khi chạy các ví dụ shell tương tác trong chương này, nếu không bạn sẽ gặp lỗi
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
0

Cuốn sách này bao gồm phiên bản 2. 3. 3 của OpenPyXL, nhưng các phiên bản mới thường xuyên được nhóm OpenPyXL phát hành. Đừng lo lắng, mặc dù. Các phiên bản mới sẽ tương thích ngược với các hướng dẫn trong cuốn sách này trong một thời gian dài. Nếu bạn có phiên bản mới hơn và muốn xem những tính năng bổ sung nào có thể có sẵn cho mình, bạn có thể xem tài liệu đầy đủ về OpenPyXL tại http. //openpyxl. đọcthedocs. tổ chức/ .

Đọc tài liệu Excel

Các ví dụ trong chương này sẽ sử dụng bảng tính có tên example. xlsx được lưu trữ trong thư mục gốc. Bạn có thể tự tạo bảng tính hoặc tải xuống từ http. //Không có tinh bột. com/automatestuff/ . hiển thị các tab cho ba trang tính mặc định có tên Trang tính 1 , Trang tính 2 và . [Số lượng sheet mặc định được tạo có thể khác nhau giữa các hệ điều hành và chương trình bảng tính. ] Sheet3 that Excel automatically provides for new workbooks. [The number of default sheets created may vary between operating systems and spreadsheet programs.]

Hình 12-1. Các tab cho trang tính của sổ làm việc nằm ở góc dưới bên trái của Excel

Trang tính 1 trong tệp ví dụ sẽ giống như. [Nếu bạn không tải xuống ví dụ. xlsx từ trang web, bạn nên tự nhập dữ liệu này vào trang tính. ]

Bảng 12-1. ví dụ. xlsx Bảng tính

 

A

B

C

1

5/4/2015 1. 34. 02 giờ chiều

Táo

73

2

5/4/2015 3. 41. 23 giờ sáng

Anh đào

85

3

6/4/2015 12. 46. 51 giờ tối

Quả lê

14

4

8/4/2015 8. 59. 43 giờ sáng

Những quả cam

52

5

4/10/2015 2. 07. 00 giờ sáng

Táo

152

6

4/10/2015 6. 10. 37 giờ chiều

chuối

23

7

4/10/2015 2. 40. 46 giờ sáng

Dâu tây

98

Bây giờ chúng ta đã có bảng tính mẫu, hãy xem cách chúng ta có thể thao tác với mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6

Mở tài liệu Excel bằng OpenPyXL

Khi bạn đã nhập mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6, bạn sẽ có thể sử dụng hàm
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
3. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]

Hàm

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
3 lấy tên tệp và trả về giá trị của kiểu dữ liệu
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
5. Đối tượng
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 này đại diện cho tệp Excel, hơi giống cách đối tượng
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
7 đại diện cho một tệp văn bản đã mở

Hãy nhớ rằng ví dụ. xlsx cần nằm trong thư mục làm việc hiện tại để bạn có thể làm việc với nó. Bạn có thể tìm ra thư mục làm việc hiện tại là gì bằng cách nhập

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
8 và sử dụng
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
9, đồng thời bạn có thể thay đổi thư mục làm việc hiện tại bằng cách sử dụng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
0.

Lấy các Sheet từ Workbook

Bạn có thể lấy danh sách tất cả các tên trang tính trong sổ làm việc bằng cách gọi phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
1. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet

Mỗi trang tính được đại diện bởi một đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2, bạn có thể lấy đối tượng này bằng cách chuyển chuỗi tên trang tính tới phương thức sổ làm việc
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
3. Cuối cùng, bạn có thể đọc biến thành viên
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
4 của đối tượng
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 để lấy trang tính hoạt động của sổ làm việc. Trang tính hiện hoạt là trang tính ở trên cùng khi sổ làm việc được mở trong Excel. Khi bạn có đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2, bạn có thể lấy tên của nó từ thuộc tính
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
7

Lấy các ô từ Sheets

Khi bạn có đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2, bạn có thể truy cập đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 theo tên của nó. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73

Đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 có thuộc tính
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
1 có chứa, không ngạc nhiên, giá trị được lưu trữ trong ô đó. Các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 cũng có các thuộc tính
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
3,
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
4 và
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
5 cung cấp thông tin vị trí cho ô

Ở đây, truy cập thuộc tính

>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
1 của đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 của chúng tôi cho ô B1 sẽ cho chúng tôi chuỗi
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
8. Thuộc tính
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
3 cho chúng ta số nguyên
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
0, thuộc tính
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
4 cho chúng ta
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
2 và thuộc tính
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
5 cho chúng ta
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
4

OpenPyXL sẽ tự động diễn giải các ngày trong cột A và trả về chúng dưới dạng giá trị

   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
5 thay vì chuỗi. Loại dữ liệu
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
5 được giải thích thêm trong Chương 16

Chỉ định một cột theo chữ cái có thể khó lập trình, đặc biệt là vì sau cột Z, các cột bắt đầu bằng cách sử dụng hai chữ cái. AA, AB, AC, v.v. Ngoài ra, bạn cũng có thể lấy một ô bằng cách sử dụng phương thức

   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
7 của trang tính và chuyển các số nguyên cho các đối số từ khóa
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
3 và
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
4 của nó. Số nguyên của hàng hoặc cột đầu tiên là
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
0, không phải
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
1. Tiếp tục ví dụ shell tương tác bằng cách nhập thông tin sau

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries

Như bạn có thể thấy, bằng cách sử dụng phương thức

   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
7 của trang tính và chuyển nó sang
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
3 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
4 sẽ giúp bạn có một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 cho ô
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
6, giống như việc chỉ định
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
7 đã làm. Sau đó, sử dụng phương thức
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
7 và các đối số từ khóa của nó, bạn có thể viết một vòng lặp
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 để in các giá trị của một loạt ô

Giả sử bạn muốn đi xuống cột B và in giá trị trong mỗi ô có số hàng lẻ. Bằng cách chuyển

   #! python3
   # readCensusExcel.py - Tabulates population and number of census tracts for
   # each county.

❶ import openpyxl, pprint
   print['Opening workbook...']
❷ wb = openpyxl.load_workbook['censuspopdata.xlsx']
❸ sheet = wb.get_sheet_by_name['Population by Census Tract']
   countyData = {}

   # TODO: Fill in countyData with each county's population and tracts.
   print['Reading rows...']
❹ for row in range[2, sheet.max_row + 1]:
       # Each row in the spreadsheet has data for one census tract.
       state  = sheet['B' + str[row]].value
       county = sheet['C' + str[row]].value
       pop    = sheet['D' + str[row]].value

   # TODO: Open a new text file and write the contents of countyData to it.
0 cho tham số "bước" của hàm
   #! python3
   # readCensusExcel.py - Tabulates population and number of census tracts for
   # each county.

❶ import openpyxl, pprint
   print['Opening workbook...']
❷ wb = openpyxl.load_workbook['censuspopdata.xlsx']
❸ sheet = wb.get_sheet_by_name['Population by Census Tract']
   countyData = {}

   # TODO: Fill in countyData with each county's population and tracts.
   print['Reading rows...']
❹ for row in range[2, sheet.max_row + 1]:
       # Each row in the spreadsheet has data for one census tract.
       state  = sheet['B' + str[row]].value
       county = sheet['C' + str[row]].value
       pop    = sheet['D' + str[row]].value

   # TODO: Open a new text file and write the contents of countyData to it.
1, bạn có thể lấy các ô từ mỗi hàng thứ hai [trong trường hợp này là tất cả các hàng được đánh số lẻ]. Biến
   #! python3
   # readCensusExcel.py - Tabulates population and number of census tracts for
   # each county.

❶ import openpyxl, pprint
   print['Opening workbook...']
❷ wb = openpyxl.load_workbook['censuspopdata.xlsx']
❸ sheet = wb.get_sheet_by_name['Population by Census Tract']
   countyData = {}

   # TODO: Fill in countyData with each county's population and tracts.
   print['Reading rows...']
❹ for row in range[2, sheet.max_row + 1]:
       # Each row in the spreadsheet has data for one census tract.
       state  = sheet['B' + str[row]].value
       county = sheet['C' + str[row]].value
       pop    = sheet['D' + str[row]].value

   # TODO: Open a new text file and write the contents of countyData to it.
3 của vòng lặp
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 được truyền cho đối số từ khóa
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
3 cho phương thức
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
7, trong khi đó ______59_______0 luôn được truyền cho đối số từ khóa
>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27
4. Lưu ý rằng số nguyên
   #! python3
   # readCensusExcel.py - Tabulates population and number of census tracts for
   # each county.

❶ import openpyxl, pprint
   print['Opening workbook...']
❷ wb = openpyxl.load_workbook['censuspopdata.xlsx']
❸ sheet = wb.get_sheet_by_name['Population by Census Tract']
   countyData = {}

   # TODO: Fill in countyData with each county's population and tracts.
   print['Reading rows...']
❹ for row in range[2, sheet.max_row + 1]:
       # Each row in the spreadsheet has data for one census tract.
       state  = sheet['B' + str[row]].value
       county = sheet['C' + str[row]].value
       pop    = sheet['D' + str[row]].value

   # TODO: Open a new text file and write the contents of countyData to it.
0, không phải chuỗi
   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---
2, được chuyển

Bạn có thể xác định kích thước của trang tính với các biến thành viên

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
01 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
02 của đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3

Lưu ý rằng phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
02 trả về một số nguyên thay vì chữ cái xuất hiện trong Excel

Chuyển đổi giữa các chữ cái và số trong cột

Để chuyển từ chữ sang số, gọi hàm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
04. Để chuyển từ số sang chữ, gọi hàm
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
05. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> from openpyxl.cell import get_column_letter, column_index_from_string
>>> get_column_letter[1]
'A'
>>> get_column_letter[2]
'B'
>>> get_column_letter[27]
'AA'
>>> get_column_letter[900]
'AHP'
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> get_column_letter[sheet.max_column]
'C'
>>> column_index_from_string['A']
1
>>> column_index_from_string['AA']
27

Sau khi bạn nhập hai hàm này từ mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
06, bạn có thể gọi
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
07 và truyền cho nó một số nguyên như 27 để tìm ra tên chữ cái của cột thứ 27 là gì. Hàm
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
08 thực hiện ngược lại. Bạn chuyển cho nó tên chữ cái của một cột và nó sẽ cho bạn biết cột đó là số mấy. Bạn không cần phải tải sổ làm việc để sử dụng các chức năng này. Nếu muốn, bạn có thể tải một sổ làm việc, lấy một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 và gọi một phương thức đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 như
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
02 để lấy một số nguyên. Sau đó, bạn có thể chuyển số nguyên đó tới
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
07

Lấy hàng và cột từ trang tính

Bạn có thể cắt

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 đối tượng để lấy tất cả
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 đối tượng trong một hàng, cột hoặc vùng hình chữ nhật của bảng tính. Sau đó, bạn có thể lặp qua tất cả các ô trong lát cắt. Nhập thông tin sau vào vỏ tương tác

   >>> import openpyxl
   >>> wb = openpyxl.load_workbook['example.xlsx']
   >>> sheet = wb.get_sheet_by_name['Sheet1']
   >>> tuple[sheet['A1':'C3']]
   [[, , ], [,
   , ], [, ,
   ]]
❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
               print[cellObj.coordinate, cellObj.value]
           print['--- END OF ROW ---']
   A1 2015-04-05 13:34:02
   B1 Apples
   C1 73
   --- END OF ROW ---
   A2 2015-04-05 03:41:23
   B2 Cherries
   C2 85
   --- END OF ROW ---
   A3 2015-04-06 12:46:51
   B3 Pears
   C3 14
   --- END OF ROW ---

Ở đây, chúng tôi xác định rằng chúng tôi muốn các đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong khu vực hình chữ nhật từ A1 đến C3 và chúng tôi nhận được một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
16 chứa các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong khu vực đó. Để giúp chúng ta hình dung đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
16 này, chúng ta có thể sử dụng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
19 trên nó để hiển thị các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 của nó trong một tuple

Bộ dữ liệu này chứa ba bộ dữ liệu. một cho mỗi hàng, từ trên cùng của khu vực mong muốn xuống dưới cùng. Mỗi bộ trong số ba bộ dữ liệu bên trong này chứa các đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong một hàng của khu vực mong muốn của chúng ta, từ ô ngoài cùng bên trái sang bên phải. Vì vậy, về tổng thể, lát trang tính của chúng ta chứa tất cả các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong khu vực từ A1 đến C3, bắt đầu từ ô trên cùng bên trái và kết thúc bằng ô dưới cùng bên phải

Để in các giá trị của từng ô trong vùng, chúng tôi sử dụng hai vòng lặp

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9. Vòng lặp
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 bên ngoài đi qua mỗi hàng trong lát cắt ❶. Sau đó, đối với mỗi hàng, vòng lặp
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 lồng nhau sẽ đi qua từng ô trong hàng đó ❷

Để truy cập giá trị của các ô trong một hàng hoặc cột cụ thể, bạn cũng có thể sử dụng thuộc tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
27 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
28 của đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries

Sử dụng thuộc tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
27 trên đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 sẽ cung cấp cho bạn một bộ gồm nhiều bộ. Mỗi bộ dữ liệu bên trong này đại diện cho một hàng và chứa
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 đối tượng trong hàng đó. Thuộc tính
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
28 cũng cung cấp cho bạn một bộ gồm nhiều bộ, với mỗi bộ bên trong chứa các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong một cột cụ thể. Đối với ví dụ. xlsx , vì có 7 hàng và 3 cột, nên
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
27 cho chúng ta một bộ gồm 7 bộ [mỗi bộ chứa 3
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 đối tượng] và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
28 cho chúng ta một bộ gồm 3 bộ [mỗi bộ chứa 7
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 .

Để truy cập một bộ dữ liệu cụ thể, bạn có thể tham khảo nó bằng chỉ mục của nó trong bộ dữ liệu lớn hơn. Ví dụ: để lấy bộ đại diện cho cột B, bạn sử dụng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
38. Để lấy bộ chứa các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong cột A, bạn sẽ sử dụng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
40. Khi bạn có một bộ đại diện cho một hàng hoặc cột, bạn có thể lặp qua các đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 của nó và in các giá trị của chúng

Sổ làm việc, Trang tính, Ô

Để xem xét nhanh, đây là danh sách tóm tắt tất cả các chức năng, phương thức và loại dữ liệu liên quan đến việc đọc một ô trong tệp bảng tính

  1. Nhập mô-đun

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    6

  2. Gọi hàm

    >>> sheet.cell[row=1, column=2]
    
    >>> sheet.cell[row=1, column=2].value
    'Apples'
    >>> for i in range[1, 8, 2]:
            print[i, sheet.cell[row=i, column=2].value]
    
    1 Apples
    3 Pears
    5 Apples
    7 Strawberries
    3

  3. Nhận một đối tượng

    >>> sheet.cell[row=1, column=2]
    
    >>> sheet.cell[row=1, column=2].value
    'Apples'
    >>> for i in range[1, 8, 2]:
            print[i, sheet.cell[row=i, column=2].value]
    
    1 Apples
    3 Pears
    5 Apples
    7 Strawberries
    6

  4. Đọc biến thành viên

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    4 hoặc gọi phương thức sổ làm việc
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    3

  5. Nhận một đối tượng

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    2

  6. Sử dụng lập chỉ mục hoặc phương pháp trang tính

       >>> import openpyxl
       >>> wb = openpyxl.load_workbook['example.xlsx']
       >>> sheet = wb.get_sheet_by_name['Sheet1']
       >>> tuple[sheet['A1':'C3']]
       [[, , ], [,
       , ], [, ,
       ]]
    ❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
                   print[cellObj.coordinate, cellObj.value]
               print['--- END OF ROW ---']
       A1 2015-04-05 13:34:02
       B1 Apples
       C1 73
       --- END OF ROW ---
       A2 2015-04-05 03:41:23
       B2 Cherries
       C2 85
       --- END OF ROW ---
       A3 2015-04-06 12:46:51
       B3 Pears
       C3 14
       --- END OF ROW ---
    7 với đối số từ khóa
    >>> import openpyxl
    >>> from openpyxl.cell import get_column_letter, column_index_from_string
    >>> get_column_letter[1]
    'A'
    >>> get_column_letter[2]
    'B'
    >>> get_column_letter[27]
    'AA'
    >>> get_column_letter[900]
    'AHP'
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> get_column_letter[sheet.max_column]
    'C'
    >>> column_index_from_string['A']
    1
    >>> column_index_from_string['AA']
    27
    3 và
    >>> import openpyxl
    >>> from openpyxl.cell import get_column_letter, column_index_from_string
    >>> get_column_letter[1]
    'A'
    >>> get_column_letter[2]
    'B'
    >>> get_column_letter[27]
    'AA'
    >>> get_column_letter[900]
    'AHP'
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> get_column_letter[sheet.max_column]
    'C'
    >>> column_index_from_string['A']
    1
    >>> column_index_from_string['AA']
    27
    4

  7. Nhận một đối tượng

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    9

  8. Đọc thuộc tính

    >>> import openpyxl
    >>> from openpyxl.cell import get_column_letter, column_index_from_string
    >>> get_column_letter[1]
    'A'
    >>> get_column_letter[2]
    'B'
    >>> get_column_letter[27]
    'AA'
    >>> get_column_letter[900]
    'AHP'
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> get_column_letter[sheet.max_column]
    'C'
    >>> column_index_from_string['A']
    1
    >>> column_index_from_string['AA']
    27
    1 của đối tượng
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    9

Dự án. Đọc dữ liệu từ bảng tính

Giả sử bạn có một bảng tính dữ liệu từ Điều tra dân số Hoa Kỳ năm 2010 và bạn có nhiệm vụ nhàm chán là duyệt qua hàng nghìn hàng của bảng tính đó để đếm cả tổng dân số và số vùng điều tra dân số cho mỗi quận. [Một vùng điều tra dân số chỉ đơn giản là một khu vực địa lý được xác định cho mục đích điều tra dân số. ] Mỗi ​​hàng đại diện cho một vùng điều tra dân số. Chúng tôi sẽ đặt tên cho tệp bảng tính dữ liệu điều tra dân số. xlsx và bạn có thể tải xuống từ http. //Không có tinh bột. com/automatestuff/ . Nội dung của nó trông giống như.

Hình 12-2. dữ liệu điều tra dân số. xlsx bảng tính

Mặc dù Excel có thể tính tổng của nhiều ô được chọn, nhưng bạn vẫn phải chọn các ô cho từng ô trong số hơn 3.000 hạt. Ngay cả khi chỉ mất vài giây để tính toán dân số của một quận bằng tay, thì việc này sẽ mất hàng giờ để tính toàn bộ bảng tính

Trong dự án này, bạn sẽ viết một tập lệnh có thể đọc từ tệp bảng tính điều tra dân số và tính toán số liệu thống kê cho từng quận chỉ trong vài giây

Đây là những gì chương trình của bạn làm

  • Đọc dữ liệu từ bảng tính Excel

  • Đếm số vùng điều tra dân số trong mỗi quận

  • Đếm tổng dân số của mỗi quận

  • In kết quả

Điều này có nghĩa là mã của bạn sẽ cần phải làm như sau

  • Mở và đọc các ô của tài liệu Excel bằng mô-đun

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    6

  • Tính toán tất cả các dữ liệu về đường và dân số và lưu trữ nó trong một cấu trúc dữ liệu

  • Ghi cấu trúc dữ liệu vào tệp văn bản bằng . py tiện ích mở rộng sử dụng mô-đun

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> type[wb]
    
    55.

Bước 1. Đọc dữ liệu bảng tính

Chỉ có một trang tính trong dữ liệu điều tra dân số. xlsx , có tên là

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
56 và mỗi hàng chứa dữ liệu cho một vùng điều tra dân số. Các cột là số vùng [A], tên viết tắt của tiểu bang [B], tên quận [C] và dân số của vùng [D].

Mở cửa sổ soạn thảo tệp mới và nhập mã sau. Lưu tệp dưới dạng readCensusExcel. p .

   #! python3
   # readCensusExcel.py - Tabulates population and number of census tracts for
   # each county.

❶ import openpyxl, pprint
   print['Opening workbook...']
❷ wb = openpyxl.load_workbook['censuspopdata.xlsx']
❸ sheet = wb.get_sheet_by_name['Population by Census Tract']
   countyData = {}

   # TODO: Fill in countyData with each county's population and tracts.
   print['Reading rows...']
❹ for row in range[2, sheet.max_row + 1]:
       # Each row in the spreadsheet has data for one census tract.
       state  = sheet['B' + str[row]].value
       county = sheet['C' + str[row]].value
       pop    = sheet['D' + str[row]].value

   # TODO: Open a new text file and write the contents of countyData to it.

Mã này nhập mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6, cũng như mô-đun
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
55 mà bạn sẽ sử dụng để in dữ liệu quận cuối cùng ❶. Sau đó, nó sẽ mở dữ liệu điều tra dân số. xlsx ❷, lấy trang tính có dữ liệu điều tra dân số ❸ và bắt đầu lặp qua các hàng của nó ❹.

Lưu ý rằng bạn cũng đã tạo một biến có tên

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59, biến này sẽ chứa dân số và số vùng mà bạn tính toán cho mỗi quận. Tuy nhiên, trước khi bạn có thể lưu trữ bất cứ thứ gì trong đó, bạn nên xác định chính xác cách bạn sẽ cấu trúc dữ liệu bên trong nó

Bước 2. Populate the Data Structure

Cấu trúc dữ liệu được lưu trữ trong

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59 sẽ là một cuốn từ điển với các chữ viết tắt trạng thái làm khóa của nó. Each state abbreviation will map to another dictionary, whose keys are strings of the county names in that state. Each county name will in turn map to a dictionary with just two keys,
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
61 and
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
62. Các khóa này ánh xạ tới số vùng điều tra dân số và dân số của quận. For example, the dictionary will look similar to this

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
0

If the previous dictionary were stored in

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59, the following expressions would evaluate like this

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
1

More generally, the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59 dictionary’s keys will look like this

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
2

Now that you know how

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59 will be structured, you can write the code that will fill it with the county data. Add the following code to the bottom of your program

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
3

The last two lines of code perform the actual calculation work, incrementing the value for

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
66 ❸ and increasing the value for
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
67 ❹ for the current county on each iteration of the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 loop

The other code is there because you cannot add a county dictionary as the value for a state abbreviation key until the key itself exists in

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59. [That is,
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
70 will cause an error if the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
71 key doesn’t exist yet. ] To make sure the state abbreviation key exists in your data structure, you need to call the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
72 method to set a value if one does not already exist for
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
73 ❶

Just as the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59 dictionary needs a dictionary as the value for each state abbreviation key, each of those dictionaries will need its own dictionary as the value for each county key ❷. And each of those dictionaries in turn will need keys
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
61 and
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
62 that start with the integer value
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
1. [If you ever lose track of the dictionary structure, look back at the example dictionary at the start of this section. ]

Since

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
72 will do nothing if the key already exists, you can call it on every iteration of the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 loop without a problem

Step 3. Write the Results to a File

After the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 loop has finished, the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59 dictionary will contain all of the population and tract information keyed by county and state. Tại thời điểm này, bạn có thể lập trình thêm mã để ghi mã này vào tệp văn bản hoặc bảng tính Excel khác. Hiện tại, chúng ta hãy chỉ sử dụng hàm
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
82 để ghi giá trị từ điển
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
59 dưới dạng một chuỗi lớn vào một tệp có tên census2010. p . Thêm đoạn mã sau vào cuối chương trình của bạn [đảm bảo giữ mã không bị thụt lề để nó nằm ngoài vòng lặp
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9].

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
4

Hàm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
82 tạo ra một chuỗi mà chính nó được định dạng là mã Python hợp lệ. Bằng cách xuất nó thành tệp văn bản có tên census2010. py , bạn đã tạo một chương trình Python từ chương trình Python của mình. Điều này có vẻ phức tạp, nhưng lợi thế là bây giờ bạn có thể nhập census2010. py giống như bất kỳ mô-đun Python nào khác. Trong trình bao tương tác, hãy thay đổi thư mục làm việc hiện tại thành thư mục có census2010 mới tạo của bạn. py [trên máy tính xách tay của tôi, đây là C. \Python34 ], rồi nhập nó.

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
5

The readCensusExcel. chương trình py là mã bỏ đi. Khi bạn đã lưu kết quả vào census2010. py , bạn sẽ không cần chạy lại chương trình. Bất cứ khi nào bạn cần dữ liệu của quận, bạn chỉ cần chạy

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
86.

Tính toán dữ liệu này bằng tay sẽ mất hàng giờ; . Sử dụng OpenPyXL, bạn sẽ không gặp khó khăn khi trích xuất thông tin được lưu vào bảng tính Excel và thực hiện các phép tính trên đó. Bạn có thể tải xuống toàn bộ chương trình từ http. //Không có tinh bột. com/automatestuff/ .

Ý tưởng cho các chương trình tương tự

Nhiều doanh nghiệp và văn phòng sử dụng Excel để lưu trữ nhiều loại dữ liệu khác nhau và không có gì lạ khi bảng tính trở nên lớn và khó sử dụng. Bất kỳ chương trình nào phân tích bảng tính Excel đều có cấu trúc tương tự. Nó tải tệp bảng tính, chuẩn bị trước một số biến hoặc cấu trúc dữ liệu, sau đó lặp qua từng hàng trong bảng tính. Một chương trình như vậy có thể làm như sau

  • So sánh dữ liệu trên nhiều hàng trong bảng tính

  • Mở nhiều tệp Excel và so sánh dữ liệu giữa các bảng tính

  • Kiểm tra xem bảng tính có hàng trống hoặc dữ liệu không hợp lệ trong bất kỳ ô nào không và thông báo cho người dùng nếu có

  • Đọc dữ liệu từ bảng tính và sử dụng nó làm đầu vào cho các chương trình Python của bạn

Viết tài liệu Excel

OpenPyXL cũng cung cấp các cách ghi dữ liệu, nghĩa là các chương trình của bạn có thể tạo và chỉnh sửa các tệp bảng tính. Với Python, thật đơn giản để tạo bảng tính với hàng nghìn hàng dữ liệu

Tạo và lưu tài liệu Excel

Gọi hàm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
87 để tạo một đối tượng
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 mới, trống. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
6

Sổ làm việc sẽ bắt đầu với một trang tính có tên Trang tính . Bạn có thể thay đổi tên của trang tính bằng cách lưu trữ một chuỗi mới trong thuộc tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
7 của nó.

Bất cứ khi nào bạn sửa đổi đối tượng

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 hoặc các trang tính và ô của nó, tệp bảng tính sẽ không được lưu cho đến khi bạn gọi phương thức sổ làm việc
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
91. Nhập thông tin sau vào trình bao tương tác [với ví dụ. xlsx trong thư mục làm việc hiện tại].

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
7

Ở đây, chúng tôi thay đổi tên của trang tính của chúng tôi. Để lưu các thay đổi của chúng tôi, chúng tôi chuyển tên tệp dưới dạng chuỗi cho phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
91. Chuyển một tên tệp khác với tên gốc, chẳng hạn như
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
93, lưu các thay đổi vào một bản sao của bảng tính

Bất cứ khi nào bạn chỉnh sửa bảng tính mà bạn đã tải từ một tệp, bạn phải luôn lưu bảng tính mới, đã chỉnh sửa thành một tên tệp khác với tên tệp gốc. Bằng cách đó, bạn sẽ vẫn có tệp bảng tính gốc để làm việc trong trường hợp lỗi trong mã của bạn khiến tệp mới, đã lưu có dữ liệu không chính xác hoặc bị hỏng

Tạo và xóa trang tính

Các trang tính có thể được thêm vào và xóa khỏi sổ làm việc bằng các phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
94 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
95. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
8

Phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
94 trả về một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 mới có tên là
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
98
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
99
, theo mặc định, đối tượng này được đặt là trang tính cuối cùng trong sổ làm việc. Theo tùy chọn, chỉ mục và tên của trang tính mới có thể được chỉ định bằng các đối số từ khóa
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
00 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
7.

Tiếp tục ví dụ trước bằng cách nhập như sau

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
9

Phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
95 lấy một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2, không phải là một chuỗi tên trang tính, làm đối số của nó. Nếu bạn chỉ biết tên của một trang tính mà bạn muốn xóa, hãy gọi
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
3 và chuyển giá trị trả về của nó vào
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
95

Hãy nhớ gọi phương thức

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
91 để lưu các thay đổi sau khi thêm trang tính vào hoặc xóa trang tính khỏi sổ làm việc

Ghi giá trị vào ô

Ghi giá trị vào ô cũng giống như ghi giá trị vào khóa trong từ điển. Nhập cái này vào shell tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
0

Nếu bạn có tọa độ của ô dưới dạng một chuỗi, bạn có thể sử dụng nó giống như một khóa từ điển trên đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 để chỉ định ô nào sẽ ghi vào

Dự án. Cập nhật bảng tính

Trong dự án này, bạn sẽ viết một chương trình để cập nhật các ô trong bảng tính bán sản phẩm. Chương trình của bạn sẽ xem qua bảng tính, tìm các loại sản phẩm cụ thể và cập nhật giá của chúng. Tải xuống bảng tính này từ http. //Không có tinh bột. com/automatestuff/ . hiển thị bảng tính trông như thế nào.

Hình 12-3. Bảng tính doanh thu sản phẩm

Mỗi hàng đại diện cho một lần bán hàng riêng lẻ. Các cột là loại sản phẩm đã bán [A], giá mỗi pound của sản phẩm đó [B], số lượng pound đã bán [C] và tổng doanh thu từ việc bán hàng [D]. Cột TOTAL được đặt thành công thức Excel =ROUND[B3*C3, 2] , công thức này nhân chi phí mỗi pound với số pound đã bán và . Với công thức này, các ô ở cột TỔNG sẽ tự động cập nhật nếu có sự thay đổi ở cột B hoặc C.

Bây giờ, hãy tưởng tượng rằng giá của tỏi, cần tây và chanh được nhập không chính xác, khiến bạn phải thực hiện nhiệm vụ nhàm chán là đi qua hàng nghìn hàng trong bảng tính này để cập nhật giá mỗi pound cho bất kỳ hàng tỏi, cần tây và chanh nào. Bạn không thể thực hiện việc tìm và thay thế giá đơn giản vì có thể có các mặt hàng khác có cùng mức giá mà bạn không muốn “sửa” nhầm. ” Đối với hàng nghìn hàng, việc này sẽ mất hàng giờ để thực hiện bằng tay. Nhưng bạn có thể viết một chương trình có thể hoàn thành việc này trong vài giây

Chương trình của bạn làm như sau

  • Vòng lặp trên tất cả các hàng

  • Nếu hàng là tỏi, cần tây hoặc chanh, hãy thay đổi giá

Điều này có nghĩa là mã của bạn sẽ cần phải làm như sau

  • Mở tệp bảng tính

  • Đối với mỗi hàng, hãy kiểm tra xem giá trị trong cột A là

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> wb.get_sheet_names[]
    ['Sheet1', 'Sheet2', 'Sheet3']
    >>> sheet = wb.get_sheet_by_name['Sheet3']
    >>> sheet
    
    >>> type[sheet] 
    >>> sheet.title
    'Sheet3'
    >>> anotherSheet = wb.active
    >>> anotherSheet
    
    08,
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> wb.get_sheet_names[]
    ['Sheet1', 'Sheet2', 'Sheet3']
    >>> sheet = wb.get_sheet_by_name['Sheet3']
    >>> sheet
    
    >>> type[sheet] 
    >>> sheet.title
    'Sheet3'
    >>> anotherSheet = wb.active
    >>> anotherSheet
    
    09 hay
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> wb.get_sheet_names[]
    ['Sheet1', 'Sheet2', 'Sheet3']
    >>> sheet = wb.get_sheet_by_name['Sheet3']
    >>> sheet
    
    >>> type[sheet] 
    >>> sheet.title
    'Sheet3'
    >>> anotherSheet = wb.active
    >>> anotherSheet
    
    10

  • Nếu có, cập nhật giá trong cột B

  • Lưu bảng tính vào một tệp mới [để bạn không bị mất bảng tính cũ, đề phòng]

Bước 1. Thiết lập cấu trúc dữ liệu với thông tin cập nhật

Giá mà bạn cần cập nhật như sau

Rau cần tây

1. 19

Tỏi

3. 07

Chanh vàng

1. 27

Bạn có thể viết mã như thế này

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
1

Có dữ liệu giá sản xuất và cập nhật được mã hóa cứng như thế này là một chút không phù hợp. Nếu bạn cần cập nhật lại bảng tính với giá khác hoặc sản phẩm khác, bạn sẽ phải thay đổi rất nhiều mã. Mỗi khi bạn thay đổi mã, bạn có nguy cơ gặp lỗi

Một giải pháp linh hoạt hơn là lưu trữ thông tin giá đã sửa trong từ điển và viết mã của bạn để sử dụng cấu trúc dữ liệu này. Trong cửa sổ soạn thảo tệp mới, nhập mã sau

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
2

Lưu cái này dưới dạng updateProduce. p . Nếu cần cập nhật lại bảng tính, bạn chỉ cần cập nhật từ điển

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
11, không phải bất kỳ mã nào khác.

Bước 2. Kiểm tra tất cả các hàng và cập nhật giá không chính xác

Phần tiếp theo của chương trình sẽ lặp qua tất cả các hàng trong bảng tính. Thêm đoạn mã sau vào cuối updateProduce. p .

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
3

Chúng tôi lặp qua các hàng bắt đầu từ hàng 2, vì hàng 1 chỉ là tiêu đề ❶. Ô ở cột 1 [nghĩa là cột A] sẽ được lưu trữ trong biến

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
12 ❷. Nếu
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
12 tồn tại dưới dạng khóa trong từ điển
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
11 ❸, thì bạn biết đây là một hàng phải được sửa giá. Giá chính xác sẽ là trong
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
15

Lưu ý cách sử dụng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
11 làm cho mã sạch sẽ như thế nào. Chỉ một câu lệnh
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
17, thay vì mã như
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
18, là cần thiết cho mọi loại sản phẩm để cập nhật. Và vì mã sử dụng từ điển
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
11 thay vì mã hóa cứng tên sản phẩm và chi phí cập nhật vào vòng lặp
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9, nên bạn chỉ sửa đổi từ điển
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
11 chứ không phải mã nếu bảng tính bán hàng sản phẩm cần thay đổi bổ sung

Sau khi xem qua toàn bộ bảng tính và thực hiện các thay đổi, mã sẽ lưu đối tượng

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 vào updatedProduceSales. xlsx ❹. Nó không ghi đè lên bảng tính cũ trong trường hợp có lỗi trong chương trình của bạn và bảng tính cập nhật bị sai. Sau khi kiểm tra xem bảng tính được cập nhật có phù hợp không, bạn có thể xóa bảng tính cũ.

Bạn có thể tải xuống toàn bộ mã nguồn của chương trình này từ http. //Không có tinh bột. com/automatestuff/ .

Ý tưởng cho các chương trình tương tự

Vì nhiều nhân viên văn phòng luôn sử dụng bảng tính Excel nên một chương trình có thể tự động chỉnh sửa và ghi tệp Excel có thể thực sự hữu ích. Một chương trình như vậy có thể làm như sau

  • Đọc dữ liệu từ một bảng tính và ghi nó vào các phần của bảng tính khác

  • Đọc dữ liệu từ các trang web, tệp văn bản hoặc khay nhớ tạm và ghi dữ liệu đó vào bảng tính

  • Tự động “dọn dẹp” dữ liệu trong bảng tính. Ví dụ: nó có thể sử dụng các biểu thức chính quy để đọc nhiều định dạng số điện thoại và chỉnh sửa chúng thành một định dạng chuẩn duy nhất

Đặt kiểu phông chữ của ô

Tạo kiểu cho các ô, hàng hoặc cột nhất định có thể giúp bạn nhấn mạnh các vùng quan trọng trong bảng tính của mình. Ví dụ, trong bảng tính sản xuất, chương trình của bạn có thể áp dụng văn bản in đậm cho các hàng khoai tây, tỏi và rau mùi tây. Hoặc có lẽ bạn muốn in nghiêng mọi hàng có giá mỗi pound lớn hơn $5. Tạo kiểu các phần của một bảng tính lớn bằng tay sẽ rất tẻ nhạt, nhưng các chương trình của bạn có thể thực hiện ngay lập tức

Để tùy chỉnh kiểu phông chữ trong các ô, quan trọng, hãy nhập hàm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
23 từ mô-đun
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
24

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
4

Điều này cho phép bạn gõ

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
23 thay vì
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
26. [Xem để xem lại phong cách tuyên bố
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
27 này. ]

Đây là một ví dụ tạo một sổ làm việc mới và đặt ô A1 có phông chữ in nghiêng 24 điểm. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
5

A cell’s style can be set by assigning the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 object to the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
29 attribute

In this example,

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
30 returns a
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 object, which is stored in
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
32 ❶. The keyword arguments to
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
23,
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
34 and
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
35, configure the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 object. And when
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
37 is assigned to the cell’s
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
38 attribute ❷, all that font styling information gets applied to cell A1

Đối tượng phông chữ

Để đặt thuộc tính kiểu phông chữ, bạn chuyển đối số từ khóa tới

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
23. hiển thị các đối số từ khóa có thể có cho hàm
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
23

Bảng 12-2. Đối số Từ khóa cho Phông chữ

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
29 Thuộc tính

Đối số từ khóa

Loại dữ liệu

Sự miêu tả

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
42

Chuỗi

Tên phông chữ, chẳng hạn như

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
43 hoặc
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
44

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
34

số nguyên

Kích thước điểm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
46

Boolean

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
47, đối với phông chữ đậm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
35

Boolean

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
47, cho phông chữ nghiêng

Bạn có thể gọi

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
23 để tạo một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 và lưu trữ đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 đó trong một biến. Sau đó, bạn chuyển nó cho
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
53, lưu trữ đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
54 kết quả trong một biến và gán biến đó cho thuộc tính
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
29 của đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9. Ví dụ: mã này tạo các kiểu phông chữ khác nhau

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
6

Ở đây, chúng tôi lưu trữ một đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 trong
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
58 và sau đó đặt thuộc tính
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
38 của đối tượng A1
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 thành
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
58. Chúng tôi lặp lại quy trình với một đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
28 khác để đặt kiểu cho ô thứ hai. Sau khi bạn chạy mã này, kiểu của các ô A1 và B3 trong bảng tính sẽ được đặt thành kiểu phông chữ tùy chỉnh, như thể hiện trong

Hình 12-4. Một bảng tính với các kiểu phông chữ tùy chỉnh

Đối với ô A1, chúng tôi đặt tên phông chữ thành

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
44 và đặt
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
46 thành
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
65, để văn bản của chúng tôi xuất hiện ở dạng in đậm Times New Roman. Chúng tôi không chỉ định kích thước, vì vậy giá trị mặc định của
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6, 11, được sử dụng. Trong ô B3, văn bản của chúng tôi được in nghiêng, với kích thước 24;

công thức

Formulas, which begin with an equal sign, can configure cells to contain values calculated from other cells. Trong phần này, bạn sẽ sử dụng mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6 để lập trình thêm công thức vào các ô, giống như bất kỳ giá trị bình thường nào. Ví dụ

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
7

This will store =SUM[B1. B8] as the value in cell B9. Thao tác này đặt ô B9 thành công thức tính tổng các giá trị trong các ô từ B1 đến B8. Bạn có thể thấy điều này trong hành động trong.

Figure 12-5. Ô B9 chứa công thức =SUM[B1. B8] , thao tác này sẽ thêm các ô từ B1 đến B8.

Một công thức được đặt giống như bất kỳ giá trị văn bản nào khác trong một ô. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
8

The cells in A1 and A2 are set to 200 and 300, respectively. Giá trị trong ô A3 được đặt thành công thức tính tổng các giá trị trong A1 và A2. When the spreadsheet is opened in Excel, A3 will display its value as 500

Các công thức Excel cung cấp một mức độ khả năng lập trình cho bảng tính nhưng có thể nhanh chóng trở nên không thể quản lý được đối với các tác vụ phức tạp. Ví dụ: ngay cả khi bạn rất quen thuộc với các công thức Excel, bạn vẫn đau đầu khi cố gắng giải mã =IFERROR[TRIM[IF[LEN[VLOOKUP[F7, Sheet2. $A$1. $B$10000, 2, FALSE]]>0,SUBSTITUTE[VLOOKUP[F7, Sheet2. $A$1. $B$10000, 2, FALSE], “ ”, “”],“”]], “”] actually does. Mã Python dễ đọc hơn nhiều.

Điều chỉnh hàng và cột

Trong Excel, việc điều chỉnh kích thước của hàng và cột dễ dàng như nhấp và kéo các cạnh của tiêu đề hàng hoặc cột. Nhưng nếu bạn cần đặt kích thước của hàng hoặc cột dựa trên nội dung của ô hoặc nếu bạn muốn đặt kích thước cho một số lượng lớn tệp bảng tính, thì việc viết chương trình Python để thực hiện sẽ nhanh hơn nhiều

Các hàng và cột cũng có thể được ẩn hoàn toàn khỏi chế độ xem. Hoặc chúng có thể được "đóng băng" tại chỗ để chúng luôn hiển thị trên màn hình và xuất hiện trên mọi trang khi bảng tính được in [rất tiện cho các tiêu đề]

Đặt chiều cao hàng và chiều rộng cột

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 đối tượng có thuộc tính
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
70 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
71 kiểm soát chiều cao của hàng và chiều rộng của cột. Nhập cái này vào shell tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
9

A sheet’s

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
70 and
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
71 are dictionary-like values;
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
70 contains
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
75 objects and
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
71 contains
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
77 objects. In
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
70, you can access one of the objects using the number of the row [in this case, 1 or 2]. In
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
71, you can access one of the objects using the letter of the column [in this case, A or B]

The dimensions. xlsx spreadsheet looks like .

Hình 12-6. Hàng 1 và cột B được đặt thành chiều cao và chiều rộng lớn hơn

Once you have the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
75 object, you can set its height. Once you have the
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
77 object, you can set its width. Chiều cao của hàng có thể được đặt thành giá trị số nguyên hoặc float trong khoảng từ
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
1 đến
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
83. Giá trị này biểu thị chiều cao được đo bằng điểm , trong đó một điểm bằng 1/72 inch. Chiều cao hàng mặc định là 12. 75. The column width can be set to an integer or float value between
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
1 and
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
85. Giá trị này biểu thị số lượng ký tự ở kích thước phông chữ mặc định [11 điểm] có thể được hiển thị trong ô. Chiều rộng cột mặc định là 8. 43 characters. Các cột có chiều rộng _______49_______1 hoặc các hàng có chiều cao _______49_______1 bị ẩn khỏi người dùng.

Hợp nhất và hủy hợp nhất các ô

Một vùng ô hình chữ nhật có thể được hợp nhất thành một ô duy nhất bằng phương pháp trang tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
88. Nhập thông tin sau vào vỏ tương tác

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
0

Đối số của

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
88 là một chuỗi gồm các ô trên cùng bên trái và dưới cùng bên phải của vùng hình chữ nhật sẽ được hợp nhất.
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
90 hợp nhất 12 ô thành một ô duy nhất. Để đặt giá trị của các ô đã hợp nhất này, chỉ cần đặt giá trị của ô trên cùng bên trái của nhóm đã hợp nhất

Khi bạn chạy mã này, đã hợp nhất. xlsx sẽ giống như.

Hình 12-7. Đã hợp nhất các ô trong bảng tính

Để hủy hợp nhất các ô, hãy gọi phương thức trang tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
91. Enter this into the interactive shell

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
1

Nếu bạn lưu các thay đổi của mình và sau đó xem bảng tính, bạn sẽ thấy rằng các ô được hợp nhất đã trở lại là các ô riêng lẻ

Panes đóng băng

For spreadsheets too large to be displayed all at once, it’s helpful to “freeze” a few of the top rows or leftmost columns onscreen. Frozen column or row headers, for example, are always visible to the user even as they scroll through the spreadsheet. Chúng được gọi là các ngăn cố định . Trong OpenPyXL, mỗi đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 có thuộc tính
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
93 có thể được đặt thành đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 hoặc một chuỗi tọa độ của ô. Lưu ý rằng tất cả các hàng ở trên và tất cả các cột ở bên trái của ô này sẽ bị cố định, nhưng bản thân hàng và cột của ô sẽ không bị cố định.

Để bỏ cố định tất cả các ngăn, hãy đặt

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
93 thành
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
96 hoặc
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
97. hiển thị những hàng và cột nào sẽ bị cố định đối với một số cài đặt ví dụ của
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
93

Table 12-3. Ví dụ về ngăn đông lạnh

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
93 setting

Hàng và cột bị cố định

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
00

Hàng 1

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
01

Column A

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
02

Cột A và B

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
03

Row 1 and columns A and B

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
04 hoặc
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
05

Không có tấm đông lạnh

Make sure you have the produce sales spreadsheet from http. //Không có tinh bột. com/automatestuff/ . Sau đó nhập thông tin sau vào Shell tương tác.

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
2

Nếu bạn đặt thuộc tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
93 thành
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
07, hàng 1 sẽ luôn có thể xem được, bất kể người dùng cuộn ở đâu trong bảng tính. Bạn có thể thấy điều này trong

Hình 12-8.  With

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> wb.get_sheet_names[]
['Sheet1', 'Sheet2', 'Sheet3']
>>> sheet = wb.get_sheet_by_name['Sheet3']
>>> sheet

>>> type[sheet] 
>>> sheet.title
'Sheet3'
>>> anotherSheet = wb.active
>>> anotherSheet
93 set to
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
07, row 1 is always visible even as the user scrolls down

biểu đồ

OpenPyXL hỗ trợ tạo biểu đồ thanh, đường, phân tán và hình tròn bằng cách sử dụng dữ liệu trong các ô của trang tính. Để tạo biểu đồ, bạn cần làm như sau

  1. Create a

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    10 object from a rectangular selection of cells

  2. Create a

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    11 object by passing in the
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    10 object

  3. Tạo một đối tượng

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    13

  4. Nối đối tượng

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    11 với đối tượng
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    13

  5. Thêm đối tượng

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet['A1']
    
    >>> sheet['A1'].value
    datetime.datetime[2015, 4, 5, 13, 34, 2]
    >>> c = sheet['B1']
    >>> c.value
    'Apples'
    >>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
    'Row 1, Column B is Apples'
    >>> 'Cell ' + c.coordinate + ' is ' + c.value
    'Cell B1 is Apples'
    >>> sheet['C1'].value
    73
    13 vào đối tượng
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    2, tùy chọn chỉ định ô nào ở góc trên cùng bên trái của biểu đồ sẽ được định vị

Đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
10 yêu cầu một số giải thích.
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
10 đối tượng được tạo bằng cách gọi hàm
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
20 và truyền ba đối số

  1. The

    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.get_sheet_by_name['Sheet1']
    >>> sheet.max_row
    7
    >>> sheet.max_column
    3
    2 object containing your chart data

  2. Một bộ gồm hai số nguyên, đại diện cho ô trên cùng bên trái của vùng chọn ô hình chữ nhật chứa dữ liệu biểu đồ của bạn. The first integer in the tuple is the row, and the second is the column. Note that

       >>> import openpyxl
       >>> wb = openpyxl.load_workbook['example.xlsx']
       >>> sheet = wb.get_sheet_by_name['Sheet1']
       >>> tuple[sheet['A1':'C3']]
       [[, , ], [,
       , ], [, ,
       ]]
    ❶ >>> for rowOfCellObjects in sheet['A1':'C3']:for cellObj in rowOfCellObjects:
                   print[cellObj.coordinate, cellObj.value]
               print['--- END OF ROW ---']
       A1 2015-04-05 13:34:02
       B1 Apples
       C1 73
       --- END OF ROW ---
       A2 2015-04-05 03:41:23
       B2 Cherries
       C2 85
       --- END OF ROW ---
       A3 2015-04-06 12:46:51
       B3 Pears
       C3 14
       --- END OF ROW ---
    0 is the first row, not
    >>> import openpyxl
    >>> wb = openpyxl.load_workbook['example.xlsx']
    >>> sheet = wb.active
    >>> sheet.columns[1]
    [, , , ,
    , , ]
    >>> for cellObj in sheet.columns[1]:
            print[cellObj.value]
    
    Apples
    Cherries
    Pears
    Oranges
    Apples
    Bananas
    Strawberries
    1

  3. Một bộ gồm hai số nguyên, đại diện cho ô dưới cùng bên phải của vùng chọn ô hình chữ nhật chứa dữ liệu biểu đồ của bạn. Số nguyên đầu tiên trong bộ dữ liệu là hàng và số thứ hai là cột

shows some sample coordinate arguments

Figure 12-9. Từ trái sang phải.

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
24;

Enter this interactive shell example to create a bar chart and add it to the spreadsheet

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
3

Điều này tạo ra một bảng tính trông giống như

Hình 12-10.  A spreadsheet with a chart added

We’ve created a bar chart by calling

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
27. Bạn cũng có thể tạo biểu đồ đường, biểu đồ phân tán và biểu đồ hình tròn bằng cách gọi
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
28,
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
29 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
30

Thật không may, trong phiên bản hiện tại của OpenPyXL [2. 3. 3], hàm

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
31 không tải biểu đồ trong tệp Excel. Ngay cả khi tệp Excel có biểu đồ, đối tượng
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 được tải sẽ không bao gồm chúng. If you load a
>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 object and immediately save it to the same . xlsx filename, you will effectively remove the charts from it.

Tóm lược

Thường thì phần khó của việc xử lý thông tin không phải là bản thân quá trình xử lý mà chỉ đơn giản là lấy dữ liệu ở định dạng phù hợp cho chương trình của bạn. Nhưng khi bạn đã tải bảng tính của mình vào Python, bạn có thể trích xuất và thao tác dữ liệu của nó nhanh hơn nhiều so với thao tác thủ công

You can also generate spreadsheets as output from your programs. So if colleagues need your text file or PDF of thousands of sales contacts transferred to a spreadsheet file, you won’t have to tediously copy and paste it all into Excel

Được trang bị mô-đun

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
6 và một số kiến ​​thức lập trình, bạn sẽ thấy việc xử lý ngay cả những bảng tính lớn nhất cũng dễ như ăn bánh

câu hỏi thực hành

Đối với các câu hỏi sau, hãy tưởng tượng bạn có một đối tượng

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
6 trong biến
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
36, đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 trong biến
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
38, đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 trong biến
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
40, đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
41 trong biến
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
42 và đối tượng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
43 trong biến
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
44


Q

1. Hàm

>>> sheet.cell[row=1, column=2]

>>> sheet.cell[row=1, column=2].value
'Apples'
>>> for i in range[1, 8, 2]:
        print[i, sheet.cell[row=i, column=2].value]

1 Apples
3 Pears
5 Apples
7 Strawberries
3 trả về cái gì?

Q

2. Phương thức sổ làm việc

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
1 trả về cái gì?

Q

3. Làm cách nào để bạn truy xuất đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 cho một trang tính có tên
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
48?

Q

4. How would you retrieve the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
2 object for the workbook’s active sheet?

Q

5. Bạn lấy giá trị trong ô C5 như thế nào?

Q

6. Bạn sẽ đặt giá trị trong ô C5 thành

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
50 như thế nào?

Q

7. Làm cách nào để bạn truy xuất hàng và cột của ô dưới dạng số nguyên?

Q

8. Các phương thức trang tính

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
02 và
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> type[wb]
01 trả về cái gì và kiểu dữ liệu của các giá trị trả về này là gì?

Q

9. If you needed to get the integer index for column

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
53, what function would you need to call?

Q

10. Nếu bạn cần lấy tên chuỗi cho cột

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
54, bạn sẽ cần gọi hàm nào?

Q

11. How can you retrieve a tuple of all the

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet.max_row
7
>>> sheet.max_column
3
9 objects from A1 to F1?

Q

12. Bạn sẽ lưu sổ làm việc vào tên tệp như thế nào ví dụ. xlsx ?

Q

13. Làm thế nào để bạn thiết lập một công thức trong một ô?

Q

15. How would you set the height of row 5 to 100?

Q

16. Làm thế nào bạn sẽ ẩn cột C?

Q

17. Kể tên một vài tính năng mà OpenPyXL 2. 3. 3 does not load from a spreadsheet file

Q

18. What is a freeze pane?

Q

19. What five functions and methods do you have to call to create a bar chart?

Dự án thực hành

For practice, write programs that perform the following tasks

Trình tạo bảng nhân

Tạo chương trình bảng nhân. py lấy một số N từ dòng lệnh và tạo một N . Ví dụ, khi chương trình được chạy như thế này. ×N multiplication table in an Excel spreadsheet. For example, when the program is run like this:

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
4

nó sẽ tạo một bảng tính trông giống như

Hình 12-11.  A multiplication table generated in a spreadsheet

Row 1 and column A should be used for labels and should be in bold

Blank Row Inserter

Tạo chương trình blankRowInserter. py that takes two integers and a filename string as command line arguments. Hãy gọi số nguyên đầu tiên N và số nguyên thứ hai M . Starting at row N , the program should insert M blank rows into the spreadsheet. Ví dụ, khi chương trình được chạy như thế này.

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
5

bảng tính “trước” và “sau” sẽ giống như

Hình 12-12. Trước [trái] và sau [phải] hai hàng trống được chèn vào hàng 3

Bạn có thể viết chương trình này bằng cách đọc nội dung của bảng tính. Sau đó, khi viết ra bảng tính mới, hãy sử dụng vòng lặp

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 để sao chép N dòng đầu tiên. Đối với các dòng còn lại, hãy thêm M vào số hàng trong bảng tính đầu ra.

Biến tần di động bảng tính

Viết chương trình đảo hàng, cột của các ô trong bảng tính. Ví dụ giá trị ở hàng 5 cột 3 sẽ ở hàng 3 cột 5 [và ngược lại]. Điều này nên được thực hiện cho tất cả các ô trong bảng tính. Ví dụ: bảng tính “trước” và “sau” sẽ trông giống như

Hình 12-13. Bảng tính trước [trên] và sau [dưới] đảo ngược

Bạn có thể viết chương trình này bằng cách sử dụng các vòng lặp

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.active
>>> sheet.columns[1]
[, , , ,
, , ]
>>> for cellObj in sheet.columns[1]:
        print[cellObj.value]

Apples
Cherries
Pears
Oranges
Apples
Bananas
Strawberries
9 lồng nhau để đọc dữ liệu của bảng tính thành một danh sách cấu trúc dữ liệu danh sách. Cấu trúc dữ liệu này có thể có
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
58 cho ô tại cột
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
59 và hàng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
60. Sau đó, khi viết ra bảng tính mới, hãy sử dụng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
61 cho ô ở cột
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
59 và hàng
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
60

Tệp văn bản vào bảng tính

Viết chương trình đọc nội dung của một số tệp văn bản [bạn có thể tự tạo các tệp văn bản] và chèn các nội dung đó vào bảng tính, mỗi dòng một dòng văn bản. Các dòng của tệp văn bản đầu tiên sẽ nằm trong các ô của cột A, các dòng của tệp văn bản thứ hai sẽ nằm trong các ô của cột B, v.v.

Sử dụng phương thức đối tượng

>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
64 để trả về danh sách các chuỗi, một chuỗi trên mỗi dòng trong tệp. Đối với tệp đầu tiên, xuất dòng đầu tiên sang cột 1, hàng 1. Dòng thứ hai phải được ghi vào cột 1, hàng 2, v.v. Tệp tiếp theo được đọc với
>>> import openpyxl
>>> wb = openpyxl.load_workbook['example.xlsx']
>>> sheet = wb.get_sheet_by_name['Sheet1']
>>> sheet['A1']

>>> sheet['A1'].value
datetime.datetime[2015, 4, 5, 13, 34, 2]
>>> c = sheet['B1']
>>> c.value
'Apples'
>>> 'Row ' + str[c.row] + ', Column ' + c.column + ' is ' + c.value
'Row 1, Column B is Apples'
>>> 'Cell ' + c.coordinate + ' is ' + c.value
'Cell B1 is Apples'
>>> sheet['C1'].value
73
65 sẽ được ghi vào cột 2, tệp tiếp theo vào cột 3, v.v.

Bảng tính thành tệp văn bản

Viết chương trình thực hiện các nhiệm vụ của chương trình trước theo thứ tự ngược lại. Chương trình sẽ mở một bảng tính và ghi các ô của cột A vào một tệp văn bản, các ô của cột B vào một tệp văn bản khác, v.v.

Tôi có thể sử dụng Python với Excel không?

Có nhiều gói Python khác nhau để làm việc với Excel. Phần lớn trong số này là để đọc và ghi các tệp Excel [e. g. openpyxl và xlsxwriter]. PyXLL rất khác với các gói khác này. Thay vì chỉ cho phép bạn đọc và ghi tệp Excel, PyXLL tích hợp Python vào Excel .

Python có thể tự động hóa Excel không?

Nhìn chung, Tự động hóa Python Excel là một quy trình đổi mới mà bạn có thể sử dụng để tạo báo cáo trực quan trên Python giống như cách bạn làm trên Excel một cách liền mạch. Organizations can leverage Python Excel Automation to leverage their business tasks according to their needs.

Làm cách nào để kết nối Python với Excel?

Các bước để nhập tệp Excel vào Python bằng Pandas .
Bước 1. Chụp đường dẫn tập tin. Trước tiên, hãy chụp lại đường dẫn đầy đủ nơi tệp Excel được lưu trữ trên máy tính của bạn. .
Bước 2. Áp dụng mã Python. .
Bước 3. Chạy mã Python để nhập tệp Excel

Tại sao tôi nên sử dụng Python với Excel?

Tự động hóa dễ dàng hơn . Sử dụng Python có thể làm điều này nhanh hơn nhiều.

Chủ Đề