Nhập dữ liệu trong Excel bằng Python

Bạn có thể dễ dàng nhập tệp Excel vào Python bằng Pandas. Để hoàn thành mục tiêu này, bạn sẽ cần sử dụng read_excel

import pandas as pd

df = pd.read_excel[r'Path where the Excel file is stored\File name.xlsx']
print[df]

Lưu ý rằng đối với phiên bản Excel cũ hơn, bạn có thể cần sử dụng phần mở rộng tệp là 'xls'

Và nếu bạn có một trang tính Excel cụ thể mà bạn muốn nhập, thì bạn có thể áp dụng

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]

Bây giờ hãy xem lại một ví dụ bao gồm dữ liệu được nhập vào Python

Dữ liệu được nhập vào Python

Giả sử rằng bạn có bảng sau được lưu trữ trong Excel [trong đó tên tệp Excel là 'sản phẩm']

product_namepricecomputer700tablet250printer120laptop1200keyboard100

Sau đó, bạn có thể làm theo các bước bên dưới để nhập tệp Excel vào Python

Các bước để nhập tệp Excel vào Python bằng Pandas

Bước 1. Chụp đường dẫn tập tin

Trước tiên, hãy chụp lại đường dẫn đầy đủ nơi tệp Excel được lưu trữ trên máy tính của bạn

Ví dụ: giả sử tệp Excel được lưu trữ theo đường dẫn sau

C. \Users\Ron\Desktop\ sản phẩm. xlsx

Trong mã Python bên dưới, bạn sẽ cần sửa đổi tên đường dẫn để phản ánh vị trí lưu trữ tệp Excel trên máy tính của bạn

Đừng quên bao gồm tên tệp [trong ví dụ của chúng tôi, đó là ‘ products ‘ được đánh dấu bằng màu xanh lam]. Bạn cũng cần bao gồm phần mở rộng tệp Excel [trong trường hợp của chúng tôi, đó là ‘ . xlsx ‘ như được đánh dấu bằng màu xanh lục].

Bước 2. Áp dụng mã Python

Đây là mã Python cho ví dụ của chúng tôi

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]

Lưu ý rằng bạn nên đặt “r” trước chuỗi đường dẫn để giải quyết các ký tự đặc biệt, chẳng hạn như ‘\’. Ngoài ra, đừng quên đặt tên tệp ở cuối đường dẫn + ‘. xlsx’

Bước 3. Chạy mã Python để nhập tệp Excel

Chạy mã Python [được điều chỉnh theo đường dẫn của bạn] và bạn sẽ nhận được tập dữ liệu sau

  product_name  price
0     computer    700
1       tablet    250
2      printer    120
3       laptop   1200
4     keyboard    100

Lưu ý rằng bạn đã nhận được kết quả giống như kết quả được lưu trữ trong tệp Excel

Ghi chú. bạn sẽ phải cài đặt gói bổ sung nếu gặp lỗi sau khi chạy mã

Nhập Lỗi. Thiếu phụ thuộc tùy chọn 'xlrd'

Sau đó, bạn có thể sử dụng phương pháp cài đặt PIP để cài đặt openpyxl cho. tập tin xlsx

pip install openpyxl

Bước tùy chọn. Chọn tập hợp con của các cột

Bây giờ nếu bạn muốn chọn một cột hoặc nhiều cột cụ thể từ tệp Excel thì sao?

Ví dụ: nếu bạn chỉ muốn chọn cột product_name thì sao?

import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]

Chạy mã [sau khi điều chỉnh đường dẫn tệp] và bạn sẽ chỉ nhận được cột product_name

  product_name
0     computer
1       tablet
2      printer
3       laptop
4     keyboard

Bạn có thể chỉ định các cột bổ sung bằng cách tách tên của chúng bằng dấu phẩy, vì vậy nếu bạn muốn bao gồm cả cột product_name và price, bạn có thể sử dụng cú pháp này

import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name', 'price']]
print[df]

Bạn sẽ cần đảm bảo rằng tên cột được chỉ định trong mã khớp chính xác với tên cột trong tệp Excel. Nếu không, bạn sẽ nhận được các giá trị NaN

Phần kết luận

Bạn vừa xem cách nhập tệp Excel vào Python bằng Pandas

Đôi khi, bạn có thể cần nhập tệp CSV vào Python. Nếu đúng như vậy, bạn có thể xem hướng dẫn sau giải thích cách nhập tệp CSV vào Python bằng Pandas

Bạn cũng có thể xem Tài liệu về Pandas để tìm hiểu thêm về các tùy chọn khác nhau mà bạn có thể áp dụng đối với read_excel

Trước khi bạn đọc bài viết này và tìm hiểu tự động hóa trong Python…. Hãy cùng xem video Christian Genco [một lập trình viên tài năng và một doanh nhân] giải thích tầm quan trọng của viết mã bằng cách lấy ví dụ về tự động hóa

Bạn có thể đã cười sảng khoái sau khi xem video này và chắc hẳn bạn cũng đã hiểu tầm quan trọng của tự động hóa trong cuộc sống thực. Bây giờ chúng ta hãy đến với chủ đề…

Chúng ta đều biết rằng Python đang thống trị trên toàn thế giới và chúng ta cũng biết rằng Python thân thiện với người mới bắt đầu và dễ học so với các ngôn ngữ khác. Một trong những điều tốt nhất bạn có thể làm với Python là Tự động hóa.  

Hãy xem xét một tình huống mà bạn được yêu cầu tạo một tài khoản trên một trang web dành cho 30.000 nhân viên. Bạn sẽ cảm thấy thế nào? . Ngoài ra, điều này sẽ mất quá nhiều thời gian và đây không phải là một quyết định thông minh.  

Bây giờ hãy tưởng tượng cuộc sống của những nhân viên làm công việc nhập liệu. Công việc của họ là lấy dữ liệu từ các bảng như Excel hoặc Google Sheet và chèn nó vào một nơi khác. Họ duyệt các trang web và tạp chí khác nhau, họ thu thập dữ liệu từ đó và sau đó chèn nó vào cơ sở dữ liệu. Họ cũng cần thực hiện các phép tính cho các mục.  

Nói chung, thu nhập dựa trên hiệu suất trong công việc này. Nhiều mục hơn, lương cao hơn [tất nhiên ai cũng muốn có mức lương cao hơn trong công việc của mình].  

Nhưng bạn có nghĩ rằng thật nhàm chán khi lặp đi lặp lại cùng một loại công việc không?

Bây giờ câu hỏi là…. “Làm sao tôi có thể làm nhanh?”, “Làm cách nào tôi có thể tự động hóa công việc của mình?”

Thay vì thực hiện các loại tác vụ này theo cách thủ công, chỉ cần dành một giờ viết mã và tự động hóa những loại công việc này để giúp cuộc sống của bạn dễ dàng hơn. Bạn có thể tự động hóa công việc tẻ nhạt của mình bằng cách viết ít dòng mã hơn trong Python.  

Trong blog này, chúng tôi sẽ tạo một dự án nhỏ để học tự động hóa trong Python. Nếu bạn là người mới bắt đầu thì bạn có thể muốn xem một số video để tìm hiểu về tự động hóa trong Python và đọc blog này có thể là một nhiệm vụ nhàm chán đối với bạn nhưng ở đây chúng tôi sẽ giải thích chi tiết mọi thứ và làm cho mọi thứ dễ dàng hơn

Sẽ thật tuyệt nếu bạn đã biết khái niệm cốt lõi của Python. Chúng tôi sẽ lấy một ví dụ về một trang tính Excel với một số mục nhập và chúng tôi sẽ tìm hiểu quy trình tự động hóa. Chúng ta sẽ viết một chương trình Python có thể xử lý hàng nghìn bảng tính trong chưa đầy một giây. Bị kích thích???

Giới thiệu nhiệm vụ

Xử lý hoặc cập nhật hàng nghìn bảng tính theo cách thủ công sẽ mất quá nhiều thời gian. Có thể mất hàng giờ, hàng ngày hoặc thậm chí hàng tháng. Chúng tôi sẽ viết một chương trình Python để tự động hóa tác vụ này. Chúng tôi sẽ làm việc trên một bảng tính được đưa ra trong hình dưới đây

Trong bảng tính này, chúng tôi có hồ sơ cho tất cả các loại giao dịch, nhưng giả sử do lỗi [lỗi con người hoặc lỗi hệ thống], giá của sản phẩm được liệt kê trong cột thứ ba là sai. Giả sử chúng ta cần giảm giá 10% [nhân giá với 0. 9 và tính toán lại giá trị]. Bạn có thể thực hiện công việc này theo cách thủ công bằng cách sử dụng công thức toán học ở cột thứ tư nhưng sẽ mất quá nhiều thời gian [có thể 1 tuần hoặc 2 tuần] nếu có hàng nghìn bản ghi.  

Chúng tôi sẽ viết một chương trình python để tự động hóa quá trình này. Ngoài ra, chúng tôi sẽ thêm một biểu đồ vào nó. Chương trình python của chúng tôi sẽ thực hiện nhiệm vụ này cho chúng tôi chỉ trong vài giây.  

Hãy tham gia vào khu vực viết mã

Để làm việc trên trang tính Excel này, chúng tôi sẽ sử dụng thư viện openpyxl. Tạo một thư mục trong thư mục của bạn, đặt tên cho nó và cài đặt gói openpyxl bằng cách thực hiện lệnh sau trong thiết bị đầu cuối của bạn

________số 8

Bây giờ chúng tôi có thể nhập gói này để làm việc trên bảng tính của mình. Trước đó, hãy thêm bảng tính vào thư mục dự án của bạn. Bây giờ hãy tạo một ứng dụng tệp. py trong thư mục của bạn và viết mã dưới đây

con trăn




import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
54
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
55

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
56
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
0
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
54
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
2

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
4
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
6______97
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
9
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
91
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
92
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
93

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
95
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
96
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
97
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
98
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
99
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
80
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
81
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
82
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
83
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
84

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
86
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
88
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
89
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
pip install openpyxl
02
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
04
pip install openpyxl
05
pip install openpyxl
06
pip install openpyxl
07
pip install openpyxl
08
pip install openpyxl
09

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
pip install openpyxl
91
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
88
pip install openpyxl
94
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
pip install openpyxl
97
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
02

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
91
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
93
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
80
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
96
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
98
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
94
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
541
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
94
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
545
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
547

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
548

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
549
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
550
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
553
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
554
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

Hãy hiểu đoạn mã trên

Chúng tôi sẽ giải thích từng bước mã được viết ở trên để hiểu toàn bộ quy trình

Bước 1. Để làm việc trên bảng tính của chúng tôi, hãy nhập gói openpyxl [chúng tôi đã sử dụng bí danh xl để làm cho mã của chúng tôi sạch hơn và ngắn hơn]. Ngoài ra, để thêm biểu đồ vào bảng tính, chúng ta cần nhập hai lớp BarChart và Reference.  

import openpyxl as xl
from openpyxl.chart import BarChart, Reference

Bước 2. Bây giờ chúng ta cần tải bảng tính python của sổ làm việc Excel. xlsx. Viết mã được đưa ra dưới đây. wb trả về đối tượng và với đối tượng này, chúng tôi đang truy cập Sheet1 từ sổ làm việc.  

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
9

Bước 3. Để truy cập vào các mục từ hàng 2 đến 4 trong cột thứ ba [mục nhập cho cột giá], chúng ta cần thêm vòng lặp for vào đó. Chúng tôi đang lưu mục này trong một ô biến.  

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
8

Bước 4. Bây giờ chúng ta cần tính giá sửa chữa. Vì vậy, chúng tôi đang nhân các giá trị được lưu trong biến ô với 0. 9. Sau khi tính toán xong, chúng ta cần thêm tất cả các giá đã sửa vào một cột mới [cột 4]. Để thêm một cột mới, chúng tôi sẽ nhận được một tham chiếu đến ô trong hàng đã cho nhưng trong cột thứ tư. Khi ô được tạo, chúng ta cần đặt các giá trị giá đã sửa trong ô này [cột thứ tư].  

pip install openpyxl
0

Bước 5. Một nửa công việc đã hoàn thành. Chúng tôi đã tính giá cập nhật và chúng tôi đã thêm giá đó vào cột thứ tư. Bây giờ chúng ta cần thêm một biểu đồ vào trang tính hiện tại. Để tạo biểu đồ, chúng ta cần chọn một dải giá trị.  

Trong dự án này, chúng tôi sẽ chọn các giá trị trong cột thứ tư [giá cập nhật] và chúng tôi sẽ sử dụng giá trị đó trong biểu đồ của mình [chúng tôi chỉ cần một loạt các số để tạo biểu đồ, vì vậy chúng tôi đã lấy ví dụ về cột thứ tư. Giá trị này có thể là bất cứ thứ gì theo yêu cầu]

Chúng ta cần sử dụng lớp tham chiếu để chọn một loạt các giá trị. Chúng ta sẽ thêm năm đối số vào hàm tạo này. Đối số đầu tiên là trang tính chúng tôi đang làm việc. Hai đối số tiếp theo min_row = 2 và max_row= sheet. max_row sẽ chọn các ô từ hàng 2 đến hàng 4. Để chọn các mục từ chỉ cột thứ tư, chúng ta cần chuyển hai đối số khác min_col=4 và max_col=4. Lưu trữ kết quả trong biến 'giá trị'

pip install openpyxl
9

Bước 6. Bây giờ chúng ta đã sẵn sàng để tạo một biểu đồ. Chúng tôi sẽ tạo một thể hiện 'biểu đồ' cho lớp BarChart. Khi điều này được tạo, hãy thêm các giá trị trong biểu đồ này. Sau đó, thêm biểu đồ này vào trang tính vào hàng 2 và cột 5 [e2].  

import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
9

Bước 7. Bây giờ chúng ta cần lưu tất cả các mục đã cập nhật và biểu đồ mà chúng ta đã tạo trong đoạn mã trên. Chúng tôi sẽ lưu cái này trong một tệp mới python-spreadsheet2. xlsx vì chúng tôi không muốn vô tình ghi đè lên tệp gốc trong trường hợp chương trình của chúng tôi có lỗi.  

Chạy chương trình của bạn và bạn đã sẵn sàng. Một tệp mới được cập nhật python-spreadhsheet2. xlsx sẽ được tạo cho bạn với giá và biểu đồ được cập nhật. Dưới đây là ảnh chụp màn hình cho cùng

Bước 8. Chương trình của chúng tôi đã hoàn tất nhưng nếu bạn sử dụng đoạn mã trên thì nó sẽ không tự động hóa quy trình của hàng nghìn bảng tính. Chương trình này chỉ dựa vào một tệp cụ thể là bảng tính python. xlsx.  

Để làm cho nó hoạt động với một số bảng tính, chúng tôi sẽ sắp xếp lại mã này và chúng tôi sẽ di chuyển mã vào bên trong một hàm. Hàm này sẽ lấy tên của tệp làm đầu vào và nó sẽ thực thi quy trình. Dưới đây là mã cập nhật cho cùng

con trăn




import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
54
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
55

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
56
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
0
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
54
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
2

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
564
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
565

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
4
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
569

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
9
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
91
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
92
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
93

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
95
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
96
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
97
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
98
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
99
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
80
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
81
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
82
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
83
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
84

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
18
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
86
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
88
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
89
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
18
pip install openpyxl
02
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
04
pip install openpyxl
05
pip install openpyxl
06
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
30
pip install openpyxl
08
pip install openpyxl
09

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
18
pip install openpyxl
91
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
88
pip install openpyxl
94
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
18
pip install openpyxl
97
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
02

import openpyxl as xl
from openpyxl.chart import BarChart, Reference
3

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
91
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
93
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
80
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
96
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

data = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx'] 
df = pd.DataFrame[data, columns=['product_name']]
print[df]
98
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
94
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
541
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
pip install openpyxl
94
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
545
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
5
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
547

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
548

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
549
import pandas as pd

df = pd.read_excel[r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name']
print[df]
550
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
8

import pandas as pd

df = pd.read_excel[r'C:\Users\Ron\Desktop\products.xlsx']
print[df]
85
import openpyxl as xl
from openpyxl.chart import BarChart, Reference
70

 

Liên kết Github cho mã có đính kèm bảng tính. Tự động hóa Python

suy nghĩ cuối cùng

Đó chỉ là một ví dụ về việc sử dụng Python để tự động hóa các tác vụ nhàm chán lặp đi lặp lại. Nhưng hãy nhớ rằng tự động hóa không chỉ là về bảng tính Excel. Có rất nhiều thứ chúng ta có thể tự động hóa. Bạn có thể tìm kiếm trên nhiều trang web khác nhau như Github và bạn có thể tự động hóa rất nhiều thứ với Python.  

Bạn có thể sử dụng Python để viết mã trong Excel không?

PyXLL là một phần bổ trợ Excel cho phép bạn chạy Python trong Excel . Sử dụng Microsoft Excel làm giao diện người dùng thân thiện với mã Python của bạn. Không có VBA, chỉ có Python.

Làm cách nào để đọc dữ liệu trong Excel bằng Python?

Đọc tệp Excel . Hàm có thể đọc các tệp từ HĐH bằng cách sử dụng đường dẫn thích hợp tới tệp. Theo mặc định, chức năng sẽ đọc Sheet1. The read_excel function of the pandas library is used read the content of an Excel file into the python environment as a pandas DataFrame. The function can read the files from the OS by using proper path to the file. By default, the function will read Sheet1.

Chủ Đề