【问题标题】:Can I use OpenXmlPowerTools in a Flask App我可以在 Flask 应用程序中使用 OpenXmlPowerTools
【发布时间】:2019-08-06 17:01:59
【问题描述】:

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

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

想知道你的想法。

【问题讨论】:

    标签: c# flask web-applications openxml openxml-powertools


    【解决方案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客户端示例如下,注意clrpythonnet提供的模块:

    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。我刚刚实现了一个包装器来提供一个简化的界面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多