# 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を提供します。
IronXL for Python は、IronXL .NET 向け の成功と人気に基づいて構築されています。
## Python用IronXLをインストールする
### 前提条件
IronXL for Python を使用するには、コンピューターに次の必須ソフトウェアがインストールされていることを確認してください。
1. **.NET 6.0 SDK** : IronXL for Python は、基盤テクノロジーとして IronXL .NET ライブラリ、具体的には .NET 6.0 に依存しています。 したがって、IronXL Python 向け を使用するには、マシンに[.NET 6.0 SDK が](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)インストールされている必要があります。
2. **Python** : Python の公式 Web サイトから最新バージョンの Python 3.x をダウンロードしてインストールします。[https://www.python.org/downloads/](https://www.python.org/downloads/) 。 インストールプロセス中に、PythonをシステムPATHに追加するオプションを選択してください。これにより、コマンドラインからアクセスできるようになります。
3. **Pip**: Pipは、通常、Python 3.4以降 for Pythonインストールにバンドルされています。 ただし、ご使用 for Pythonインストールによっては、pipがすでにインストールされているかどうかを確認するか、別途インストールする必要がある場合があります。
4. **IronXLライブラリ:** IronXLライブラリはpip経由で追加できます。pipを使ってIronXLをインストールするには、以下のコマンドを実行してください。
```shell
:ProductInstall
```
[[t:(IronXLの特定バージョンをインストールするには、次の構文を使用してください: `==2023.x.x`。 例えば、次のコマンドを実行できます: `pip install ironxl==2023.x.x`。)]]
[[i:(一部のシステムでは、Python 2.xがデフォルトバージョンである場合があります。)このような場合は、pipの代わりにpip3コマンドを使用して、Python 3専用のPipを使用していることを確認してください。)]] このような場合、Python 3のPipを使用していることを確認するために、`pip3`コマンドを使用する必要があります。)}]
## Excelドキュメントを読む
IronXL for Python を使用して Excel ファイルからデータを読み取るには、数行のコードが必要です。
```python
# 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}'")
```
## 新しいExcelドキュメントを作成する
Python で Excel ドキュメントを作成するために、IronXL Python 向け はシンプルで高速なインターフェースを提供します。
```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としてエクスポートする
一般的な構造化スプレッドシート ファイル形式で保存またはエクスポートすることもできます。
```python
# 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 オブジェクトを使用してスタイルを設定できます。
```python
# Set cell's value and styles
workSheet["A1"].Value = "Hello World"
workSheet["A2"].Style.BottomBorder.SetColor("#ff6600")
workSheet["A2"].Style.BottomBorder.Type = BorderType.Double
```
## 範囲のソート
IronXL for Python を使用すると、Range を使用して Excel セルの範囲を並べ替えることができます。
```python
# 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の数式を編集することは、開始位置に`=`の等号記号を指定して値を割り当てるのと同様に簡単です。 数式はライブで計算されます。
```python
# Set a formula
workSheet["A1"].Formula = "=SUM(A2:A10)"
# Get the calculated value
sum_ = workSheet["A1"].DecimalValue
```
## Python に IronXL を選ぶ理由
IronXL for Python には、開発者が Excel ドキュメントを読み書きするための簡単な API が備わっています。
IronXL for Python では、Excel ドキュメントにアクセスするために、サーバー上に Microsoft Excel をインストールしたり、Excel Interop を使用したりする必要はありません。 これにより、Python で Excel ファイルを操作する作業が非常に迅速かつ簡単になります。
## ライセンスとサポートの提供
**IronXL Python 向け**は開発環境で無料で使用およびテストできます。
ライブ プロジェクトで使用するには、[ライセンスを購入してください](/python/excel/licensing/)。 [30 日間の試用ライセンス](trial-license)もご利用いただけます。
コード例、チュートリアル、ライセンス情報、ドキュメントの完全なリストについては、 [IronXL Python 向け](/python/excel/)をご覧ください。
サポートやお問い合わせに関しては、[私たちのチームにお尋ねください](#live-chat-support)。
IronXL for Python を使用して Excel ファイルからデータを読み取るには、数行のコードが必要です。
# Load the necessary module from IronXLfrom ironxl importWorkBook# 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 workbookworksheet = 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 outputprint(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}'")
# 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}'")
from ironxl importWorkBook, ExcelFileFormat, BorderType# Import necessary classes from ironxl# Create a new Excel WorkBook document in XLSX formatworkbook = WorkBook.create(ExcelFileFormat.XLSX)# Set metadata for the workbookworkbook.metadata.author = "IronXL"# Add a new blank worksheet named "main_sheet" to the workbookworksheet = 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 colorworksheet["A2"].style.bottom_border.set_color("#ff6600")worksheet["A2"].style.bottom_border.type = BorderType.double# Save the Excel file with the specified filenameworkbook.save_as("NewExcelFile.xlsx")
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")
Python
CSV、XLS、XLSX、JSON、XMLとしてエクスポートする
一般的な構造化スプレッドシート ファイル形式で保存またはエクスポートすることもできます。
# Assuming workSheet is an existing instance of WorkSheetworkSheet.SaveAs("NewExcelFile.xls")workSheet.SaveAs("NewExcelFile.xlsx")workSheet.SaveAsCsv("NewExcelFile.csv")workSheet.SaveAsJson("NewExcelFile.json")workSheet.SaveAsXml("NewExcelFile.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")
Python
セルと範囲をスタイリングする
Excel のセルと範囲は、Style オブジェクトを使用してスタイルを設定できます。
# Set cell's value and stylesworkSheet["A1"].Value = "Hello World"workSheet["A2"].Style.BottomBorder.SetColor("#ff6600")workSheet["A2"].Style.BottomBorder.Type = BorderType.Double
# 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 for Python を使用すると、Range を使用して Excel セルの範囲を並べ替えることができます。
# Import IronXL library for handling Excel filesfrom ironxl importWorkBook# Load an existing Excel workbook# 'sample.xls' is the file name of the Excel workbook to be loadedworkbook = 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 0worksheet = 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 A8selected_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 largestselected_range.SortAscending()# Save the changes made to the workbook, including the sorted range# The workbook's state is updated with the changes after executionworkbook.Save()
# 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()