Python용 IronXL 소개
IronXL for Python은 Iron Software 에서 개발한 강력한 라이브러리로, 소프트웨어 엔지니어가 Python 3 프로젝트에서 Excel(XLS, XLSX 및 CSV) 파일을 생성, 읽기 및 편집할 수 있도록 지원합니다.
IronXL for Python은 서버에 Excel을 설치하거나 Interop을 사용하지 않아도 됩니다. IronXL for Python은 Microsoft.Office.Interop.Excel보다 빠르고 직관적인 API를 제공합니다.
Python용 IronXL .NET 용 IronXL 의 성공과 인기를 기반으로 구축되었습니다.
Python용 IronXL 설치하세요.
필수 구성 요소
Python용 IronXL 사용하려면 컴퓨터에 다음 필수 소프트웨어가 설치되어 있는지 확인하십시오.
- .NET 6.0 SDK: IronXL for Python은 IronXL .NET 라이브러리를 기반으로 하며, 특히 .NET 6.0을 사용합니다. .NET 6.0 SDK를 머신에 설치해야 IronXL for Python을 사용할 수 있습니다.
- Python: 최신 버전 Python 3.x를 공식 Python 웹사이트에서 다운로드하고 설치하세요: https://www.python.org/downloads/. 설치 과정에서 Python을 시스템 PATH에 추가하는 옵션을 선택해야 합니다. 이렇게 하면 명령줄에서 Python에 접근할 수 있습니다.
- Pip: Pip은 보통 Python 3.4 버전 이후의 Python 설치와 함께 제공됩니다. 하지만 Python 설치 환경에 따라 pip가 이미 설치되어 있는지 확인하거나 별도로 설치해야 할 수도 있습니다.
- IronXL 라이브러리: IronXL 라이브러리는 pip를 통해 추가할 수 있습니다. 아래 명령어를 사용하여 pip로 IronXL 설치하세요.
==2023.x.x. 예를 들어 명령어 pip install ironxl==2023.x.x를 실행할 수 있습니다.pip 대신 pip3를 명시적으로 사용할 필요가 있을 수 있습니다.엑셀 문서 읽기
Python용 IronXL 사용하여 Excel 파일에서 데이터를 읽는 데는 몇 줄의 코드만 필요합니다.
# Load the necessary module from IronXL
from ironxl import WorkBook
# Load an existing Excel spreadsheet
# Replace 'sample.xlsx' with the path to your Excel file as needed.
workbook = WorkBook.load("sample.xlsx")
# Select the first worksheet from the workbook
worksheet = workbook.worksheets[0]
# Access cell A2 and get its integer value
# Ensure the correct method or property is used to fetch the integer value.
# Use 'value' to directly access the cell content.
cell_value = worksheet["A2"].value
# Print out the value of the cell A2
# Utilizing formatted strings for clear output
print(f"Cell A2 has value '{cell_value}'")
# Iterate over a range of cells and print their address and text content
# The range is defined from A2 to B10, which captures all rows in this interval.
for cell in worksheet.range("A2:B10"):
# Access each cell in the specified range
# AddressString is used to get the cell's location as a string, and Text to get its content.
print(f"Cell {cell.address} has value '{cell.text}'")
새 엑셀 문서 만들기
Python으로 엑셀 문서를 생성하려면 IronXL for Python이 간단하고 빠른 인터페이스를 제공합니다.
from ironxl import WorkBook, ExcelFileFormat, BorderType # Import necessary classes from ironxl
# Create a new Excel WorkBook document in XLSX format
workbook = WorkBook.create(ExcelFileFormat.XLSX)
# Set metadata for the workbook
workbook.metadata.author = "IronXL"
# Add a new blank worksheet named "main_sheet" to the workbook
worksheet = workbook.create_worksheet("main_sheet")
# Add data to cell "A1"
worksheet["A1"].value = "Hello World"
# Set the style for cell "A2" with a double bottom border and a specific color
worksheet["A2"].style.bottom_border.set_color("#ff6600")
worksheet["A2"].style.bottom_border.type = BorderType.double
# Save the Excel file with the specified filename
workbook.save_as("NewExcelFile.xlsx")
CSV, XLS, XLSX, JSON 또는 XML 형식으로 내보내기
또한 다양한 일반적인 스프레드시트 파일 형식으로 저장하거나 내보낼 수 있습니다.
# Assuming workSheet is an existing instance of WorkSheet
workSheet.SaveAs("NewExcelFile.xls")
workSheet.SaveAs("NewExcelFile.xlsx")
workSheet.SaveAsCsv("NewExcelFile.csv")
workSheet.SaveAsJson("NewExcelFile.json")
workSheet.SaveAsXml("NewExcelFile.xml")
셀 및 범위 스타일링
Excel 셀과 범위는 Style 객체를 사용하여 스타일을 지정할 수 있습니다.
# Set cell's value and styles
workSheet["A1"].Value = "Hello World"
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600")
workSheet["A2"].Style.BottomBorder.Type = BorderType.Double
정렬 범위
Python용 IronXL 사용하면 Range 함수를 이용하여 Excel 셀 범위를 정렬할 수 있습니다.
# Import IronXL library for handling Excel files
from ironxl import WorkBook
# Load an existing Excel workbook
# 'sample.xls' is the file name of the Excel workbook to be loaded
workbook = WorkBook.Load("sample.xls")
# Access the first worksheet in the workbook
# WorkSheets is the collection of all sheets in the workbook,
# and we select the first one using index 0
worksheet = workbook.WorkSheets[0]
# Select a range of cells from A2 to A8 in the worksheet
# This specifies a contiguous range of cells starting from A2 and ending at A8
selected_range = worksheet["A2:A8"]
# Sort the selected range of cells in ascending order
# This operation reorders the values in the specified range from smallest to largest
selected_range.SortAscending()
# Save the changes made to the workbook, including the sorted range
# The workbook's state is updated with the changes after execution
workbook.Save()
수식 편집
Excel 수식을 편집하는 것은 = 시작 부분에 등호를 할당하는 것만큼 쉽습니다. 해당 공식은 실시간으로 계산됩니다.
# Set a formula
workSheet["A1"].Formula = "=SUM(A2:A10)"
# Get the calculated value
sum_ = workSheet["A1"].DecimalValue
Python 개발에 IronXL 선택해야 하는 이유는 무엇일까요?
IronXL for Python은 개발자가 Excel 문서를 읽고 쓸 수 있는 간편한 API를 제공합니다.
Python용 IronXL 서버에 Microsoft Excel을 설치하거나 Excel 문서에 액세스하기 위해 Excel Interop을 설치할 필요가 없습니다. 이 덕분에 Python에서 엑셀 파일을 다루는 작업이 매우 빠르고 간단해집니다.
라이선스 및 지원 가능
IronXL for Python은 개발 환경에서 무료로 사용하고 테스트할 수 있습니다.
실제 프로젝트에 사용하려면 라이선스를 구매하세요. 30일 체험판 라이선스도 제공됩니다.
전체 코드 예제 목록, 튜토리얼, 라이선스 정보 및 문서는 IronXL for Python을 방문하세요.
더 많은 지원 및 문의사항은 팀에 문의하세요.
커티스 차우는 칼턴 대학교에서 컴퓨터 과학 학사 학위를 취득했으며, Node.js, TypeScript, JavaScript, React를 전문으로 하는 프론트엔드 개발자입니다. 직관적이고 미적으로 뛰어난 사용자 인터페이스를 만드는 데 열정을 가진 그는 최신 프레임워크를 활용하고, 잘 구성되고 시각적으로 매력적인 매뉴얼을 제작하는 것을 즐깁니다.