BCL easyConverter SDK
easyConverter SDK Usermanual
PDF-to-Excel Programming API  |  Download Free Trial  |  Contact Us to Purchase

Native Python API

In addition to the standard COM API, easyConverter SDK is also available as a native Python API. The interface layer is written in 100% Python 3 code, which is not compatible with Python 2. The API does not rely on any COM objects.

Although customers may freely study the full Python source code, we strongly discourage everyone from making any modifications to this code. Note that the Python layer is just an Application Binary Interface, which is subject to change without notice.

Usage

The native Python API is a thin interface around a binary executable. It is very similar to the native .NET API, with the exception of some language-specific differences.

Note: there is no native Python 2 API. That is a very different language than Python 3.

In order to use PDF2Excel, just import the library by adding import PDF2Excel. The PDF2Excel SDK is inside the PDF2Excel. namespace. PDF2Excel.py can be found under C:\Program Files\BCL Technologies\easyConverter SDK 5\Rtf.

When an error occurs, an exception of type PDF2ExcelException is thrown.

The PDF2Excel class has a destructor, which means it is automatically disposed when the object goes out of scope. That makes memory management trivial, just make sure you are not holding on to the variable when you are no longer using the SDK.

If the customer's application crashes, the system automatically closes all pipes belonging to the process, which means all related worker processes automatically quit as well.

The minimal Python sample code looks like this:

import PDF2Excel
pdf2excel = PDF2Excel.PDF2Excel()
try:
   pdf2excel.ConvertToExcel("c:\\test\\input.pdf", "c:\\test\\output.csv")
except PDF2Excel.PDF2ExcelException as ex:
   print(ex)

PDF2Excel's constructor is designed to never throw exceptions. That's because it does not launch a worker process and does not create a named pipe, it merely initializes a few variables to their default values. In other words, creating a PDF2Excel object is extremely lightweight, like creating a Color object.

However, as soon as you do anything else, it instantly launches a worker process.

Notification Events

Notification events in the native Python API are slightly different than in the native .NET API. Here is an example that catches OnPageStart, which is called before converting each page:

def PageStart(pageNumber, pageCount, fileName):
   print("Page %d of %d..." % (pageNumber, pageCount))
   return PDF2Excel.cnvResponse.CNV_CONTINUE

pdf2excel = PDF2Excel.PDF2Excel()
try:
   pdf2excel.OnPageStart = PageStart
   pdf2excel.ConvertToExcel("c:\\test\\input.pdf", "c:\\test\\output.csv")
except PDF2Excel.PDF2ExcelException as ex:
   print(ex)

Alternatively, the event handler can be a class method:

class Handler:
   def PageStart(self, pageNumber, pageCount, fileName):
      print("Page %d of %d..." % (pageNumber, pageCount))
      return PDF2Excel.cnvResponse.CNV_CONTINUE

pdf2excel = PDF2Excel.PDF2Excel()
handler = Handler()
try:
   pdf2excel.OnPageStart = handler.PageStart
   pdf2excel.ConvertToExcel("c:\\test\\input.pdf", "c:\\test\\output.csv")
except PDF2Excel.PDF2ExcelException as ex:
   print(ex)

Launch Timeout

Launching the worker process and connecting the named pipe should happen very fast, in milliseconds. However, under an abnormally heavy load, the computer may not have enough cycles to perform this task in a timely fashion.

The Native Python API has an internal timeout built in. The default value is 1 minute. This is because a server may momentarily slow down so much that it is not responding for seconds, but it usually recovers after a while.

The default timeout value can be changed via PDF2Excel's launchTimeout member (in milliseconds). This must be performed immediately after the creation of the PDF2Excel object.

pdf2excel = PDF2Excel.PDF2Excel()
try:
   pdf2excel.launchTimeout = 60000
   pdf2excel.ConvertToExcel("c:\\test\\input.pdf", "c:\\test\\output.csv")
except PDF2Excel.PDF2ExcelException as ex:
   print(ex)

Upon timeout an exception is thrown, and the SDK cannot be used. Another attempt may be made later.