c# - 用于运行 C# 代码的 Python 包装器

标签 c# python pdf coordinates wrapper

我想使用 python 读取 pdf 特定页面中特定行的坐标。但是,我无法找到合适的库来执行此操作。因此,我在 C# 中使用下面提到的这段代码。任何人都可以帮助我在 python 中找到一个包装器,通过它该代码可以在 python 中运行。

代码:

using System;
using System.Drawing;
using Bytescout.PDFExtractor;

<span data-scayt_word="namespace" data-scaytid="18">namespace</span> <span data-scayt_word="FindText" data-scaytid="19">FindText</span>
{
    class Program
    {
        static void Main(string[] <span data-scayt_word="args" data-scaytid="43">args</span>)
        {
            // Create Bytescout.PDFExtractor.TextExtractor instance
            <span data-scayt_word="TextExtractor" data-scaytid="20">TextExtractor</span> extractor = new <span data-scayt_word="TextExtractor" data-scaytid="21">TextExtractor</span>();
            extractor.RegistrationName = "demo";
            extractor.RegistrationKey = "demo";

            // Load sample PDF document
            extractor.LoadDocumentFromFile("sample1.pdf");

            <span data-scayt_word="int" data-scaytid="22">int</span> <span data-scayt_word="pageCount" data-scaytid="48">pageCount</span> = extractor.GetPageCount();
            <span data-scayt_word="RectangleF" data-scaytid="50">RectangleF</span> location;

            for (<span data-scayt_word="int" data-scaytid="23">int</span> i = 0; i < <span data-scayt_word="pageCount" data-scaytid="49">pageCount</span>; i++)
            {
                // Search each page for "<span data-scayt_word="ipsum" data-scaytid="24">ipsum</span>" string
                if (extractor.Find(i, "<span data-scayt_word="ipsum" data-scaytid="25">ipsum</span>", false, out location))
                {
                    do
                    {
                        Console.WriteLine("Found on page " + i + " at location " + location.ToString());

                    }
                    while (extractor.FindNext(out location));
                }
            }

            Console.WriteLine();
            Console.WriteLine("Press any key to continue...");
            Console.ReadLine();
        }
    }
}

最佳答案

我看到三个选项供您从 python 程序运行此代码(假设您使用的是 Windows):

最好:如果您可以使用 IronPython 解释器(请参阅 ironpython.net ),您可以直接从 python 代码使用 PDFExtractor 类:

import clr    
clr.AddReferenceToFileAndPath('c:\\path\\to\\pdfextractor.dll')
from Bytescount.PDFExtractor import TextExtractor
extractor = TextExtractor()
extractor.RegistrationName = 'demo'
# etc

或者:在运行之前使用 C# 编译器 csc.exe 编译 C# 程序(将 C# 程序另存为 Extract.cs,确保它接受 pdf 文件的路径作为输入参数):

import os,tempfile,shutil
csc = 'c:\\WINDOWS\\Microsoft.Net\\Framework64\\v4.0.30319\\csc.exe' # Or somewhere else, see below
filename = 'c:\\path\\to\\pdffile.pdf'
tempdir = tempfile.mkdtemp(prefix='Extract-temp-')
os.system(csc + ' /t:exe /out:' + tempdir + '\\Extract.exe c:\\path\\to\\Extract.cs /r:c:\\path\\to\\PDFExtractor.dll')
with os.popen(tempdir + '\\Extract.exe '+filename) as F:
    extractResult = F.read()
shutil.rmtree(tempdir)
print(extractResult)

直到 .NET Framework 版本 4.5/C# 5,csc.exe 包含在框架安装中。要获取支持 C# 6.0 的 csc.exe 版本,请参阅stackoverflow.com/questions/39089426 .

最后,您可以使用 ctypes 和“非托管导出(DllExport for .Net)”NuGet 包直接从 CPython 调用 C# 程序集,如 stackoverflow.com/questions/7367976 中所述。 .

编辑基于denfromufa的评论:从python编写PDFExtractor脚本的最佳方法是在CPython中使用pythonnet(您可以通过python -m pip install pythonnet在Windows上安装它) >) 通过这种方法,您上面的 C# 程序可以替换为以下脚本(使用 python 2.7、win32 测试):

import clr
# 'import System'  will work here (must be after 'import clr')
# You can also import System.Drawing and other .NET namespaces
clr.AddReference(r'c:\path\to\Bytescout.PDFExtractor.dll')
from Bytescout.PDFExtractor import TextExtractor
extractor = TextExtractor()
extractor.RegistrationName = 'demo'
extractor.RegistrationKey = 'demo'
extractor.LoadDocumentFromFile(r'c:\path\to\mydoc.pdf')
pageCount = extractor.GetPageCount()
for i in range(pageCount):
    result = extractor.Find(i,"somestring",False)
    while (result):
        print('Found on page '+str(i)+' on location '+str(extractor.FoundText.Bounds))
        result = extractor.FindNext()

关于c# - 用于运行 C# 代码的 Python 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44042317/

相关文章:

c# - 在 Winforms C# 中编辑 Appsettings

c# - 如何获取远程机器上正在运行的进程的描述?

c# - 仅在可见时延迟加载 System.Windows.Control.Image

python - 夹层 CMS 中如何定义页面实例的 slug?

python - 在 Python 中将文本文件拆分为列和行

python - 如何在 PyTorch 中检索多行中的 topk 值及其各自的索引?

php - 用 PHP 在 PDF 模板文档上书写/绘图

ios 5 uiwebview 阴影

java - 从java服务器获取pdf页面,并嵌入到现有的html中

c# - 是否可以在 web API 2 Controller post 方法中分别传递多个复杂类型参数?