1

是否可以在 Flask/Python 3 网络应用程序中使用基于 C#/.Net 的库,例如OpenXmlPowerTools ?

我的研究告诉我,我也许可以通过包装器?还是创建一个微服务?

想知道你的想法。

4

1 回答 1

1

你可以用pythonnet做到这一点。我在 C# 中创建了一个示例类,它引用了OpenXmlPowerToolsDocumentFormat.OpenXml程序集并提供了两种方法:

using OpenXmlPowerTools;
using DocumentFormat.OpenXml.Packaging;

namespace CodeSnippets.OpenXmlWrapper
{
    public class OpenXmlPowerToolsWrapper
    {
        public static string GetMainDocumentPart(string path)
        {
            using WordprocessingDocument wordDocument = WordprocessingDocument.Open(path, true);
            return wordDocument.MainDocumentPart.GetXElement().ToString();
        }

        public static string FinishReview(string path)
        {
            using WordprocessingDocument wordDocument = WordprocessingDocument.Open(path, true);

            var settings = new SimplifyMarkupSettings
            {
                AcceptRevisions = true,
                RemoveComments = true
            };

            MarkupSimplifier.SimplifyMarkup(wordDocument, settings);
            return wordDocument.MainDocumentPart.GetXElement().ToString();
        }
    }
}

相应的示例 Python 客户端如下所示,注意这clr是由以下提供的模块pythonnet

import clr
import shutil

clr.AddReference(
    r"..\CodeSnippets.OpenXmlWrapper\bin\x64\Debug\net471\CodeSnippets.OpenXmlWrapper")

from CodeSnippets.OpenXmlWrapper import OpenXmlPowerToolsWrapper

wrapper = OpenXmlPowerToolsWrapper()

# Display contents before finishing review, showing that the document contains revision markup.
xml_with_revision_markup = wrapper.GetMainDocumentPart("DocumentWithRevisionMarkup.docx")
print("Document before finishing review:\n")
print(xml_with_revision_markup)

# Finish review, removing all revision markup.
print("\nFinishing review ...")
shutil.copyfile("DocumentWithRevisionMarkup.docx", "Result.docx")
xml_without_revision_markup = wrapper.FinishReview("Result.docx")

# Display contents after finishing review, showing that the revision markup was removed.
print("\nDocument after finishing review:\n")
print(xml_without_revision_markup)

您可以在我的CodeSnippets GitHub 存储库中找到完整的源代码。查看CodeSnippets.OpenXmlWrapperCodeSnippets.OpenXmlWrapper.PythonClient项目。

根据您的用例,您还可以直接使用 OpenXmlPowerTools。我刚刚实现了一个包装器来提供一个简化的界面。

于 2019-11-24T16:02:12.117 回答