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 736 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 736 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/ và 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 736 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 Strawberries0
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
Quả 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 736
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 736, 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 Strawberries3. 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 Strawberries3 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 Strawberries5. Đố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 Strawberries6 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 Strawberries7 đại diện cho 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 Strawberries8 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 Strawberries9, đồ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['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 7360.
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['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 7361. 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 738
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['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 7362, 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['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 7363. 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['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 7364 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 Strawberries6 để 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['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 7362, 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['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 7367
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['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 7362, 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['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 7369 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['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 7369 có thuộc tí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 7391 chứa giá trị được lưu trữ trong ô đó, không có gì ngạc nhiên. Cá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 7369 cũng có các thuộc tí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 7393,
>>> 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 7394 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 7395 cung cấp thông tin vị trí cho ô
Ở đây, truy cập thuộc tí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 7391 của đố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 7369 của chúng tôi cho ô B1 sẽ cho chúng tôi chuỗ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 7398. Thuộc tí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 7393 cho chúng ta số nguyên
>>> import openpyxl
80, thuộc tí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 7394 cho chúng ta
>>> import openpyxl
82 và thuộc tí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 7395 cho chúng ta
>>> import openpyxl
84OpenPyXL sẽ tự động giải thích các ngày trong cột A và trả về chúng dưới dạng giá trị
>>> import openpyxl
85 thay vì chuỗi. Loại dữ liệu >>> import openpyxl
85 được giải thích thêm trong Chương 16Chỉ đị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
87 của trang tính và chuyển các số nguyên cho các đối số từ khó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 7393 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 7394 của nó. Số nguyên của hàng hoặc cột đầu tiên là
>>> import openpyxl
80, không 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 7391. 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, sử dụng phương thức
>>> import openpyxl
87 của trang tính và chuyển nó sang ________ 493 và ________ 494 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['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 7369 cho ô
>>> 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 7396, giống như việc chỉ định ________ 497 đã làm. Sau đó, sử dụng phương thức
>>> import openpyxl
87 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.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 7399 để 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
>>> 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 Strawberries90 cho tham số "bước" của 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 Strawberries91, 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
>>> 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 Strawberries93 của vòng lặp
>>> 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 7399 được truyền cho đối số từ khó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 7393 cho phương thức
>>> import openpyxl
87, trong khi đó, >>> 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 Strawberries90 luôn được truyền cho đối số từ khó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 7394. Lưu ý rằng số nguyên
>>> 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 Strawberries90, không phải chuỗi
>>> import openpyxl
82, được truyềnBạn có thể xác định kích thước của trang tính bằng 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['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 7362. 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 736
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 >>> 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 739
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['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 7362 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['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 7362 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 cá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 7362 để lấy tất cả cá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 7369 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
8Ở đâ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['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 7369 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['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 7369 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['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 7369 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['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 7369 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['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 7369 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.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 7399. Vòng lặp
>>> 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 7399 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.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 7399 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['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 7362. 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 739
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['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 7362 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 cá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 7369 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['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 7369 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 đố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 7369] 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['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 7369] .
Để 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['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 7369 trong cột A, bạn sẽ sử dụng
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]40. Sau 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['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 7369 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
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
6Gọ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
3Nhậ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Đọc biến thành viê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
64 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['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
63Nhận 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
62Sử dụng lập chỉ mục hoặc phương pháp trang tính
>>> import openpyxl
87 với đối số từ khó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
93 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
94Nhận 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
69Đọc thuộc tí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
91 của đố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
69
Dự định. Đọ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ó một nhiệm vụ nhàm chán là duyệt qua hàng nghìn hàng của bảng tính đó để đếm 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
6Tí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 .
>>> 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 Strawberries9
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 736, 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 là
>>> 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. Điền vào cấu trúc dữ liệu
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 từ điển với các chữ viết tắt trạng thái làm khóa của nó. Mỗi chữ viết tắt của tiểu bang sẽ ánh xạ tới một từ điển khác, có khóa là các chuỗi tên quận trong tiểu bang đó. Mỗi tên quận sẽ lần lượt ánh xạ tới một từ điển chỉ với hai khóa,
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]61 và
>>> 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. Ví dụ, từ điển sẽ trông giống như thế này
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]0
Nếu từ điển trước được lưu trữ trong
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]59, các biểu thức sau sẽ đánh giá như thế này
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]1
Tổng quát hơn, các khóa của từ điển
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]59 sẽ trông như thế này
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]2
Bây giờ bạn đã biết cách cấu trúc của
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]59, bạn có thể viết mã để điền dữ liệu quận vào đó. Thêm đoạn mã sau vào dưới cùng của chương trình của bạn
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]3
Hai dòng mã cuối cùng thực hiện công việc tính toán thực tế, tăng giá trị cho
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]66 ❸ và tăng giá trị cho
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]67 ❹ cho quận hiện tại trên mỗi lần lặp của vòng lặp
>>> 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 7399
Mã khác ở đó vì bạn không thể thêm từ điển quận làm giá trị cho khóa viết tắt của tiểu bang cho đến khi chính khóa đó tồn tại trong
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]59. [Tức là,
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]70 sẽ gây ra lỗi nếu khóa
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]71 chưa tồn tại. ] Để đảm bảo khóa viết tắt trạng thái tồn tại trong cấu trúc dữ liệu của bạn, bạn cần gọi phương thức
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]72 để đặt giá trị nếu một giá trị chưa tồn tại cho
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]73 ❶
Giống như từ điển
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]59 cần một từ điển làm giá trị cho mỗi khóa viết tắt của tiểu bang, mỗi những từ điển đó sẽ cần từ điển riêng của nó như . Và lần lượt mỗi những từ điển đó sẽ cần các khóa
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]61 và
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]62 bắt đầu bằng giá trị số nguyê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 7391. [Nếu bạn không nhớ cấu trúc từ điển, hãy xem lại từ điển ví dụ ở đầu phần này. ]
Vì
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]72 sẽ không làm gì nếu khóa đã tồn tại, nên bạn có thể gọi nó trên mỗi lần lặp của vòng lặp
>>> 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 7399 mà không gặp vấn đề gì
Bước 3. Ghi kết quả vào tệp
Sau khi vòng lặp
>>> 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 7399 kết thúc, từ điển
>>> import openpyxl >>> wb = openpyxl.load_workbook['example.xlsx'] >>> type[wb]59 sẽ chứa tất cả thông tin về dân số và vùng được khóa theo quận và tiểu bang. 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.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 7399].
>>> 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, 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 Strawberries6 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['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 7367 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 Strawberries6 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 ban đầu. 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 pháp
>>> 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['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 7362 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 , đối tượng này theo mặc định đượ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'] >>> 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 73800 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 7367.
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['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 7362, 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 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['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 7363 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'] >>> 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 7380
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['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 7362 để chỉ định ô nào sẽ ghi vào
Dự định. 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 sai”. ” Đố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'] >>> 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
808,>>> 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
809 hay>>> 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
810Nế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'] >>> 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 7381
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'] >>> 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 7382
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 ________ 1811, 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'] >>> 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 7383
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 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 73812 ❷. Nếu
>>> 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 73812 tồn tại dưới dạng khóa trong từ điể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 73811 ❸, thì bạn biết đây là hàng phải được sửa giá. Giá chính xác sẽ có trong
>>> 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 73815
Lưu ý cách 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 73811 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'] >>> 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 73817, thay vì mã 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 73818, 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'] >>> 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 73811 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.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 7399, nên bạn chỉ sửa đổi từ điể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 73811 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 Strawberries6 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 ô, quan trọng, hãy nhập 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 73823 từ 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 73824
>>> 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 7384
Điều này cho phép bạn gõ
>>> 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 73823 thay 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 73826. [Xem để xem lại phong cách tuyên 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 73827 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'] >>> 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 7385
Có thể đặt kiểu của ô bằng cách gán đố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 73828 cho thuộc tí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 73829
Trong ví dụ 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 73830 trả về 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 73828, được lưu trữ trong
>>> 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 73832 ❶. Các đối số từ khóa cho
>>> 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 73823,
>>> 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 73834 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 73835, định cấu hình đố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 73828. Và khi
>>> 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 73837 được gán cho thuộc tính ❷ ❷ của ô, tất cả thông tin kiểu dáng phông chữ đó sẽ được áp dụng cho ô A1
Đối tượng phông chữ
Để đặt thuộc tính kiểu phông chữ, bạn chuyển các đối số từ khóa tớ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 73823. hiển thị các đối số từ khóa có thể có cho 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 73823
Bảng 12-2. Đối số từ khóa cho thuộc tính phông 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 73829
Đối số từ khóa
Loại dữ liệu
Sự miêu 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 73842
Chuỗi
Tên phông chữ, chẳng hạn 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 73843 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 73844
>>> 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 73834
số nguyên
Kích thước điể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 73846
Boolean
>>> 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 73847, cho phông chữ đậ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 73835
Boolean
>>> 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 73847, cho phông chữ nghiêng
Bạn có thể 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 73823 để 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 73828 và lưu trữ đố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 73828 đó trong một biến. Sau đó, bạn chuyển nó cho
>>> 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 73853, lưu trữ đố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 73854 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'] >>> 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 73829 của đố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 7369. 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'] >>> 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 7386
Ở đây, chúng tôi lưu trữ 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 73828 trong
>>> 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 73858 và sau đó đặt thuộc tí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 73838 của đối tượng A1
>>> 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 7369 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 73858. 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'] >>> 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 73828 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'] >>> 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 73844 và đặ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 73846 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 73865, để 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 736, 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
Các công thức bắt đầu bằng dấu bằng có thể định cấu hình các ô để chứa các giá trị được tính toán từ các ô khác. 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 736 để 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'] >>> 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 7387
Điều này sẽ lưu trữ =SUM[B1. B8] làm giá trị trong ô 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.
Hình 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'] >>> 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 7388
Các ô trong A1 và A2 được đặt lần lượt là 200 và 300. Giá trị trong ô A3 được đặt thành công thức tính tổng các giá trị trong A1 và A2. Khi bảng tính được mở trong Excel, A3 sẽ hiển thị giá trị của nó là 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], “ ”, “”],“”]], “”] thực sự có. 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 các ô 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
Cá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 7362 có các thuộc tí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 73870 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 73871 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'] >>> 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 7389
>>> 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 73870 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 73871 của một trang tính là các giá trị giống như từ điển; . Trong
>>> 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 73870, bạn có thể truy cập một trong các đối tượng bằng số của hàng [trong trường hợp này là 1 hoặc 2]. Trong
>>> 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 73871, bạn có thể truy cập một trong các đối tượng bằng chữ cái của cột [trong trường hợp này là A hoặc B]
Các thứ nguyên. xlsx bảng tính trông giống như.
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
Khi bạn 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 73875, bạn có thể đặt chiều cao của nó. Khi bạn 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 73877, bạn có thể đặt chiều rộng của nó. Chiều cao của hàng có thể được đặt thành một giá trị số nguyên hoặc float trong khoảng 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 7391 đế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 73883. 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. Chiều rộng cột có thể được đặt thành giá trị số nguyên hoặc số float trong khoảng 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 7391 đế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 73885. 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 ký tự. Các cột có chiều rộng _______491 hoặc các hàng có chiều cao _______491 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'] >>> 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 73888. 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 730
Đối số 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 73888 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'] >>> 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 73890 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 ô, được gọi là phương pháp trang tính ________ 1891. Nhập cái này 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 731
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
Đối với các bảng tính quá lớn để hiển thị tất cả cùng một lúc, sẽ rất hữu ích nếu bạn "đóng băng" một số hàng trên cùng hoặc cột ngoài cùng bên trái trên màn hình. Ví dụ: tiêu đề cột hoặc hàng được cố định luôn hiển thị với người dùng ngay cả khi họ cuộn qua bảng tính. 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['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 7362 có thuộc tí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 73893 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['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 7369 hoặc 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.
Để giải phóng tất cả các ngăn, hãy đặ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 73893 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 73896 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 73897. hiển thị những hàng và cột nào sẽ bị đóng băng đối với một số cài đặt ví dụ 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 73893
Bảng 12-3. Ví dụ về ngăn đông lạnh
Cài đặ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 73893
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 7300
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 7301
Cột 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 7302
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 7303
Hàng 1 và 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 7304 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 7305
Không có tấm đông lạnh
Đảm bảo bạn có bảng tính bán hàng nông sản từ 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 732
Nếu bạn đặt thuộc tí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 73893 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 7307, 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. 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 73893 được đặt 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 7307, hàng 1 luôn hiển thị ngay cả khi người dùng cuộn xuống
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
Tạo đố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 từ vùng chọn ô hình chữ nhậtTạ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
11 bằng cách truyền vào đố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
10Tạ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
13Nố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
13Thê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['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
62, 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 7310 yêu cầu một số giải thích. Cá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 7310 đượ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 7320 và truyền ba đối số
Đố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
62 chứa dữ liệu biểu đồ của bạnMộ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. Số nguyên đầu tiên trong bộ dữ liệu là hàng và số thứ hai là cột. Lưu ý rằng
>>> import openpyxl
80 là hàng đầu tiên, không 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
91Mộ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
hiển thị một số đối số tọa độ mẫu
Hình 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 7324;
Nhập ví dụ trình bao tương tác này để tạo biểu đồ thanh và thêm nó vào bảng tí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 733
Điều này tạo ra một bảng tính trông giống như
Hình 12-10. Một bảng tính có thêm biểu đồ
Chúng tôi đã tạo một biểu đồ thanh 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 7327. 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 7328,
>>> 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 7329 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 7330
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 7331 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 Strawberries6 được tải sẽ không bao gồm chúng. Nếu bạn tải 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 Strawberries6 và ngay lập tức lưu nó vào cùng . xlsx , bạn sẽ xóa các biểu đồ khỏi nó một cách hiệu quả.
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
Bạn cũng có thể tạo bảng tính dưới dạng đầu ra từ các chương trình của mình. Vì vậy, nếu đồng nghiệp cần tệp văn bản hoặc tệp PDF của hàng nghìn liên hệ bán hàng được chuyển sang tệp bảng tính, bạn sẽ không phải sao chép và dán tất cả vào Excel một cách tẻ nhạt
Đượ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 736 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ó đố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 Strawberries6 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 7336, đố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 7362 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 7338, đố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 7369 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 7340, đố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 7341 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 7342 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 7343 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 7344
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 Strawberries3 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['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 7361 trả về cái gì?
Q
3. Bạn sẽ lấy đố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 7362 cho một trang tính có tên là
>>> 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 7348 như thế nào?
Q
4. 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['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 7362 cho trang tính đang hoạt động của sổ làm việc?
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 7350 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. Nếu bạn cần lấy chỉ số nguyên 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 7353, bạn sẽ cần gọi hàm nào?
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 7354, bạn cần gọi hàm nào?
Q
11. Làm cách nào bạn có thể truy xuất một bộ gồm tất cả cá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 7369 từ A1 đến 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. Bạn sẽ đặt chiều cao của hàng 5 thành 100 như thế nào?
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 không tải từ tệp bảng tính
Q
18. Ngăn đóng băng là gì?
Q
19. Bạn phải gọi năm chức năng và phương pháp nào để tạo biểu đồ thanh?
Dự án thực hành
Để thực hành, hãy viết chương trình thực hiện các tác vụ sau
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 734
nó sẽ tạo một bảng tính trông giống như
Hình 12-11. Bảng cửu chương được tạo trong bảng tính
Hàng 1 và cột A nên được sử dụng cho nhãn và phải được in đậm
Chèn hàng trống
Tạo chương trình blankRowInserter. py lấy hai số nguyên và một chuỗi tên tệp làm đối số dòng lệnh. Hãy gọi số nguyên đầu tiên N và số nguyên thứ hai M . Bắt đầu từ hàng N , chương trình sẽ chèn M hàng trống vào . 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 735
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.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 7399 để 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 khi đảo ngược [dưới]
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.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 7399 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 7358 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 7359 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 7360. 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 7361 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 7359 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 7360
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 7364 để 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 bằ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 7365 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.