Port Python-DocX function to C#
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Built a Python function, to take a MSWord document as an input, as well as a section heading/number, and return the table from that section as a JSON object.

Need to convert this python function over to C#, and use the Aspose.Words for .NET library.

Link to Python Function : https://pastebin.com/vrHkk9s0

All code samples should be posted to pastebin only, with an expiration of 1 month.

Can you provide me an example of docx file you want to convert to json and also the corresponding json output expected.Also there are several functions which function you want to convert to c#.Thank you
Codeword over 5 years ago
Link to example file https://expirebox.com/download/517144c607fb23682e5f75d1089c05c3.html. The parseDocX function. It leverages all others though, for certain aspects of the job, so they'll all essentially need to be ported.
tw0shay over 5 years ago
okay, I'm translating the code to c# and If everything goes well You will get it soon. thank you
Codeword over 5 years ago
Hello tw0shay, Would there be any problem if I use another open source library so that I can convert the Docx file to XML and then parse the XML, This is also the exact method followed in the python file.
Codeword over 5 years ago
Hey tw0shay, I just want to let you know that I have almost completed your code.As you wanted It uses aspose words . There are still some more to-do and once I have completed it, i'll let you know.Thanks
Codeword over 5 years ago
awarded to Codeword

Crowdsource coding tasks.

2 Solutions


Instead of rewriting the code, You can however run Python from within your .Net App.

Using IronPython you can actually integrate .Net Code into your Python script and for example set a string property from you Python script.

Link: https://blogs.msdn.microsoft.com/charlie/2009/10/25/running-ironpython-scripts-from-a-c-4-0-program/

Alternately, you can just run your Python script from C# and get the result of the python function.

You can try something like this:

static void Main()
    var ipy = Python.CreateRuntime();
    dynamic parser = ipy.UseFile("WordParser.py");
    string docPath = "C:\\SomeFolder\\SomeWord.Docx";
    var result = parser.parseDocX(docPath,"someSection");

i hope you liked my solution, it can be such a big earn of time!

Hi Houcem ; i had planned on using IronPython, but the Python-Docx library uses the LXML module, which requires C-extensions. Because of this, the IronPython won't be able to run the functions. I know an alternative might be to package python 3.x with the solution, but won't be able to support or be able to do that with the machines where this program will be deployed.
tw0shay over 5 years ago
i don't know if i'm getting it right or not, if it requires to execute '.c' files, you can install MinGW, so machines can run the '.c' file properly. Otherwise, i think it will work properly with C# 4.0 as listed in the Official Microsoft Blog (link posted in bounty) . Another solution, is to use another library that supports many languages, so you can do whatever you want, with the same library (link: http://docxfactory.com/ ) or use this good library for .NET (link https://docx.codeplex.com/ ) . hope these could be helpful ;)
Chlegou over 5 years ago

Hello tw0shay,Good news, I have completed you c# code successfully, now the question is how would you like to receive it mail or github?

Hey you can download the code from here https://github.com/Tech-engine/tw0shay Also, a lot of hard work has been put and you will notice that in the code, So I would very much appreciate if you could tip me as well. Thank you and enjoy. With warm regards, Codeword
Codeword over 5 years ago
c# code, pastebin link https://pastebin.com/bjUgHENT
Codeword over 5 years ago
This works well. Thank you Codeword, i appreciate the level of detail on conversion you went thru.
tw0shay over 5 years ago
Thank you, and glad you liked it. tw0shay
Codeword over 5 years ago
View Timeline