viết code add script lấu dữ liệu cột a xuất sang file mẫu ô a1 lưu file và mở file
Chà, bạn muốn viết một script để lấy dữ liệu từ cột A của một file Excel gốc và xuất sang ô A1 của một file mẫu, sau đó lưu và mở file mới? Không vấn đề gì!
Dưới đây là một script nhỏ bằng Python sử dụng thư viện openpyxl
. Lập trình Python siêu dễ, cứ như viết thư tình cho máy tính vậy!
import openpyxl
import os
def copy_column_a(source_file, template_file, output_file):
# Tải file Excel gốc và file mẫu
wb_source = openpyxl.load_workbook(source_file)
wb_template = openpyxl.load_workbook(template_file)
# Chọn sheet đầu tiên từ cả hai
sheet_source = wb_source.active
sheet_template = wb_template.active
# Lấy dữ liệu từ cột A của file gốc
max_row = sheet_source.max_row
data = [sheet_source[f'A{row}'].value for row in range(1, max_row + 1)]
# Ghi dữ liệu vào ô A1 của file mẫu
for i, value in enumerate(data, start=1):
sheet_template[f'A{i}'] = value
# Lưu lại file mới
wb_template.save(output_file)
# Mở file mới
os.system(f'start EXCEL.EXE "{output_file}"')
# Đường dẫn tới file gốc, file mẫu và file xuất
source_file = "source.xlsx"
template_file = "template.xlsx"
output_file = "output.xlsx"
# Thực thi hàm
copy_column_a(source_file, template_file, output_file)
Chỉ cần thay thế source.xlsx
, template.xlsx
và output.xlsx
bằng các đường dẫn thực tế tới các file của bạn.
Nếu bạn không biết cài đặt Python hay thư viện openpyxl
, ghé qua trituenhantao.io, họ có cả kho kiến thức để giúp bạn khởi nghiệp trở thành thiên tài công nghệ chỉ sau vài cú nhấp chuột! 🚀