ワークブックからOLEオブジェクトを抽出
Contents
[
Hide
]
時には、WorkbookからOLEオブジェクトを抽出する必要があります。Aspose.Cells for Python via .NETは、そのOleオブジェクトの抽出と保存をサポートしています。
この記事は、少数のコード行でVisual Studio.Netでコンソールアプリケーションを作成し、ワークブックから異なるOLEオブジェクトを抽出する方法を示しています。
ワークブックからOLEオブジェクトを抽出
テンプレートワークブックの作成
- Microsoft Excelでワークブックを作成しました。
- 最初のワークシートにMicrosoft Wordドキュメント、Excelワークブック、PDFドキュメントをOLEオブジェクトとして追加しました。
OLEオブジェクトを含むテンプレートドキュメント(OleFile.xls) |
---|
![]() |
次に、OLEオブジェクトを抽出し、それらをそれぞれのファイルタイプでハードディスクに保存します。
Aspose.Cells for Python Excelライブラリを使用したOLEオブジェクトの抽出
以下のコードは実際にOLEオブジェクトを検出して抽出する実際の作業を行います。OLEオブジェクト(DOC、XLS、PDFファイル)はディスクに保存されます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import FileFormatType, Workbook | |
from io import BytesIO | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Open the template file. | |
workbook = Workbook(dataDir + "oleFile.xlsx") | |
# Get the OleObject Collection in the first worksheet. | |
oles = workbook.worksheets[0].ole_objects | |
# Loop through all the oleobjects and extract each object in the worksheet. | |
for i in range(len(oles)): | |
ole = oles[i] | |
# Specify the output filename. | |
fileName = dataDir + "outOle" + str(i) + "." | |
# Specify each file format based on the oleobject format type. | |
if ole.file_format_type == FileFormatType.DOC: | |
fileName = "doc" | |
elif ole.file_format_type == FileFormatType.EXCEL_97_TO_2003: | |
fileName = "Xlsx" | |
elif ole.file_format_type == FileFormatType.PPT: | |
fileName = "Ppt" | |
elif ole.file_format_type == FileFormatType.PDF: | |
fileName = "Pdf" | |
elif ole.file_format_type == FileFormatType.UNKNOWN: | |
fileName = "Jpg" | |
# Save the oleobject as a new excel file if the object type is xls. | |
if ole.file_format_type == FileFormatType.XLSX: | |
ms = BytesIO() | |
if ole.object_data != None: | |
ms.write(ole.object_data) | |
oleBook = Workbook(ms) | |
oleBook.settings.is_hidden = False | |
oleBook.save(dataDir + "outOle" + str(i) + ".out.xlsx") | |
else: | |
if ole.object_data != None: | |
fs = open(fileName, "wb") | |
fs.write(ole.object_data) | |
fs.close() |