Đã hỏi 2 năm, 10 tháng trước 2 years, 10 months ago
Đã xem 4k lần 4k times
Có cách nào để chỉ định bảng nào để mở trong sổ làm việc Excel khi sử dụng lệnh Python để mở ứng dụng không? [Ex: Sử dụng Win32 Dispatch hoặc Os.System]?
hỏi ngày 9 tháng 1 năm 2020 lúc 18:59Jan 9, 2020 at 18:59
2
Tôi nghĩ rằng cách tốt nhất sẽ là kích hoạt sự tập trung vào trang tính trước, sau đó mở sổ làm việc.
from openpyxl import load_workbook
wb = load_workbook['my_workbook.xlsx']
sheet_to_focus = 'my_sheet'
for s in range[len[wb.sheetnames]]:
if wb.sheetnames[s] == sheet_to_focus:
break
wb.active = s
wb.save['my_workbook.xlsx']
Sau đó, bạn có thể có thể mở nó [mã chưa được kiểm tra]:
import os
os.chdir['C:\\my_folder\\subfolder']
os.system['start excel.exe my_workbook.xlsx']
Đã trả lời ngày 9 tháng 1 năm 2020 lúc 19:31Jan 9, 2020 at 19:31
7
Câu hỏi
Tính năng "Chèn vào mã" giúp chúng tôi tạo mã để truy cập tệp Excel dưới dạng tập dữ liệu trên Notebook Jupyter. Tuy nhiên, nó không thể tạo mã để mở một bảng cụ thể từ tệp Excel với nhiều bảng. Hãy cho tôi biết làm thế nào để truy cập một tờ cụ thể?
Gây ra
Do các mã của gấu trúc được tạo bởi tính năng "Chèn vào mã" không bao gồm tham số sheet_name cho hàm read_excel [], nó đã trả lại tờ đầu tiên từ bảng excel chứa nhiều bảng.sheet_name parameter for the read_excel[] function, it returned the first sheet from the Excel sheet that contains multiple sheets.
Câu trả lời
Bạn cần thêm tham số sheet_name vào mã chức năng & nbsp; pandas 'read_excel [] để truy cập bảng cụ thể từ tệp excel chứa nhiều bảng.sheet_name parameter to the generated pandas' read_excel[] function code to access specific sheet from Excel file that contains multiple sheets.
. "Nhãn": "Phần mềm IBM w \/o tps"}, "sản phẩm": {"mã": "sshut6", "nhãn": "IBM Watson Studio Premium Hộp mực cho IBM Cloud Pak cho dữ liệu"}, "Danh mục ARM ". "," Nhãn ":" Nền tảng độc lập "}]," Phiên bản ":" Tất cả các phiên bản "}]]
Trong hướng dẫn này, chúng ta sẽ thấy một cuộc biểu tình về cách sử dụng các tờ Excel trong Python bằng OpenPyXL.
Thành lập
Thực hiện lệnh bên dưới để cài đặt gói Python cần thiết.
pip install openpyxl
Tạo một tờ Excel
# import Workbook
from openpyxl import Workbook# create Workbook object
wb=Workbook[]# set file path
filepath="/home/ubuntu/demo.xlsx"# save workbook
wb.save[filepath]
Điều này sẽ tạo một tệp excel mới demo.xlsx.
Trống demo.xlsxThêm dữ liệu vào bảng Excel
Viết vào một ô
# import load_workbookđầu ra của mã trên
from openpyxl import load_workbook# set file path
filepath="/home/ubuntu/demo.xlsx"# load demo.xlsx
wb=load_workbook[filepath]# select demo.xlsx
sheet=wb.active# set value for cell A1=1
sheet['A1'] = 1# set value for cell B2=2
sheet.cell[row=2, column=2].value = 2# save workbook
wb.save[filepath]
Nối thêm nhóm các giá trị ở dưới cùng của bảng hiện tại
# import Workbookđầu ra của chương trình trên
from openpyxl import Workbook# create Workbook object
wb=Workbook[]# set file path
filepath="/home/ubuntu/demo.xlsx"# select demo.xlsx
sheet=wb.activedata=[['Id','Name','Marks'],
[1,ABC,50],
[2,CDE,100]]# append all rows
for row in data:
sheet.append[row]# save file
wb.save[filepath]
Đọc từ một tờ Excel
Đọc một ô
# import load_workbook
from openpyxl import load_workbook# set file path
filepath="/home/ubuntu/demo.xlsx"# load demo.xlsx
wb=load_workbook[filepath]# select demo.xlsx
sheet=wb.active# get b1 cell value
b1=sheet['B1']# get b2 cell value
b2=sheet['B2']# get b3 cell value
b3=sheet.cell[row=3,column=2]# print b1, b2 and b3
print[b1.value]print[b2.value]print[b3.value]
Đầu ra của mã trên:
Name
ABC
DEF
Lặp lại bằng các hàng
________số 8Đầu ra của mã trên:
Id | Name | Marks |1 | ABC | 50 |2 | CDE | 100 |
Lặp lại bằng các hàng
________số 8
Thêm một tờ vào XLSX hiện cóChúng ta có thể thêm tờ 2 vào demo.xlsx bằng mã dưới đây.
import os
os.chdir['C:\\my_folder\\subfolder']
os.system['start excel.exe my_workbook.xlsx']
1import os
os.chdir['C:\\my_folder\\subfolder']
os.system['start excel.exe my_workbook.xlsx']
0 Đầu ra của mã trênSao chép dữ liệu từ trang này sang trang khácTừ mã trên, tạo một tờ mới với cùng một dữ liệu với trang tính.
Đầu ra của mã trên
Xóa bảng khỏi XLSX hiện cóChúng ta có thể xóa tờ 2 khỏi demo.xlsx bằng mã bên dưới.
2 đầu ra của mã trên and also share it with your friends. It means a lot to me.import os
os.chdir['C:\\my_folder\\subfolder']
os.system['start excel.exe my_workbook.xlsx']
Bạn có muốn kiểm tra các bài viết khác của tôi không?