Skip to main content
Shawn Waldie
Dec 16, 2022 12:00 PM UTC

Creating a Word document with tables

I'm in the very beginning stages of designing/developing a program that will create a file or files that can be opened/imported by Word to display multiple tables like these (screenshot from a word document):

I can handle part that has to calculate these details.

However, i'm not sure what my options are for generating a file that can be imported/read by Word.

It appears that Word can handle XML documents, so I'm wondering if that would be worth researching.

Our current process is very labor intensive and requires a lot of hands-on, so ideally, my program would generate the file(s), and Word could then open and display the info seen above with minimal manual massaging.

Any ideas would be appreciated.



------------------------------
Shawn Waldie
enterprise application developer
Rocket Forum Shared Account
Los Angeles CA US
------------------------------

    15 replies

    Ian McGowan
    Participating Frequently
    Dec 20, 2022 12:37 AM UTC
    I have not tried this, but if it were doing it I would explore something similar to the RTF approach, but take a docx file that would act as the template, unzip it, grab the word/document.xml file, swap in your table, and zip it back up again.

    ------------------------------
    Henry Unger
    President
    Hitech Systems Inc
    Encino CA US
    ------------------------------
    I've done the RTF search and replace for document creation, but it does get very messy with tables that have a variable number of rows.  If you can fix the max number of rows ahead of time, that's the simplest approach.

    Creating your own word .docx file is difficult, but not impossible.  I've spent a lot of time creating a subroutine that will take a query and create a .xlsx file - a real one, with formatting and formulas and everything.  You could do the same thing for word, if you grab a word document, rename to .zip, unzip it, you can view the xml contents.  To create a document from scratch you do the opposite and create a directory, jam in the appropriate xml files, zip it up and rename to .docx.  It's not trivial to do from pure basic - much easier on a windows machine with helper libraries from msft, but it's possible.

    ------------------------------
    Ian McGowan
    Principal Consultant
    Rocket Forum Shared Account
    ------------------------------
    Shawn Waldie
    Dec 20, 2022 11:50 AM UTC
    I've done the RTF search and replace for document creation, but it does get very messy with tables that have a variable number of rows.  If you can fix the max number of rows ahead of time, that's the simplest approach.

    Creating your own word .docx file is difficult, but not impossible.  I've spent a lot of time creating a subroutine that will take a query and create a .xlsx file - a real one, with formatting and formulas and everything.  You could do the same thing for word, if you grab a word document, rename to .zip, unzip it, you can view the xml contents.  To create a document from scratch you do the opposite and create a directory, jam in the appropriate xml files, zip it up and rename to .docx.  It's not trivial to do from pure basic - much easier on a windows machine with helper libraries from msft, but it's possible.

    ------------------------------
    Ian McGowan
    Principal Consultant
    Rocket Forum Shared Account
    ------------------------------

    Impressive. And thanks for making that code available!

    Also, fortunately, the table sizes are fixed in this case.



    ------------------------------
    Shawn Waldie
    enterprise application developer
    Rocket Forum Shared Account
    Los Angeles CA US
    ------------------------------
    Shawn Waldie
    Dec 20, 2022 11:51 AM UTC
    Word can read Open Document Format files. They are well defined from either Open Office, Apache, or Libre Office.

    ------------------------------
    Mark A Baldridge
    Principal Consultant
    Thought Mirror
    Nacogdoches, Texas United States
    ------------------------------
    That's right.  I had forgotten about that.
    Thank you!

    ------------------------------
    Shawn Waldie
    enterprise application developer
    Rocket Forum Shared Account
    Los Angeles CA US
    ------------------------------
    Sam Powell
    Participating Frequently
    Dec 22, 2022 5:14 PM UTC
    I'm in the very beginning stages of designing/developing a program that will create a file or files that can be opened/imported by Word to display multiple tables like these (screenshot from a word document):

    I can handle part that has to calculate these details.

    However, i'm not sure what my options are for generating a file that can be imported/read by Word.

    It appears that Word can handle XML documents, so I'm wondering if that would be worth researching.

    Our current process is very labor intensive and requires a lot of hands-on, so ideally, my program would generate the file(s), and Word could then open and display the info seen above with minimal manual massaging.

    Any ideas would be appreciated.



    ------------------------------
    Shawn Waldie
    enterprise application developer
    Rocket Forum Shared Account
    Los Angeles CA US
    ------------------------------
    Hi Shawn:

    You could try creating an html document simple enough to do then send the output through a python routine that will convert an html to a true .doc document.

    We have done that with Excel XML documents and converted them to true ".xlsx" Excel documents to avoid the extension mismatch error that Excel gives.

    Do a convert HTML to Word in Python and you'll find quite a bit of info.

    Regards, Sam

    ------------------------------
    Samuel Powell
    President/Developer
    Advanced Transportation Systems Inc
    Colorado Springs CO US
    ------------------------------
    Shawn Waldie
    Jun 12, 2023 10:11 AM UTC
    I'm in the very beginning stages of designing/developing a program that will create a file or files that can be opened/imported by Word to display multiple tables like these (screenshot from a word document):

    I can handle part that has to calculate these details.

    However, i'm not sure what my options are for generating a file that can be imported/read by Word.

    It appears that Word can handle XML documents, so I'm wondering if that would be worth researching.

    Our current process is very labor intensive and requires a lot of hands-on, so ideally, my program would generate the file(s), and Word could then open and display the info seen above with minimal manual massaging.

    Any ideas would be appreciated.



    ------------------------------
    Shawn Waldie
    enterprise application developer
    Rocket Forum Shared Account
    Los Angeles CA US
    ------------------------------

    It's been a while, but this project is finally complete and now in use.

    I thought I should reach out with a 'heart-felt thank you' to everyone who offered suggestions. I needed help to get it off the ground and your feedback provided that.

    I ended up going with the generation of the document.xml for a Word document since I was able to locate all  the positions where variables would be updated.

    The old manual process required hours to complete and was error-prone. This new method requires less than a minute...a huge payoff for the users.



    ------------------------------
    Shawn Waldie
    enterprise application developer
    Rocket Forum Shared Account
    Los Angeles CA US
    ------------------------------