Long story short, I want to build a system that reorders some components in a document file (be it a docx or odt, I don’t have a hard constraint atm).

So my problem input should be a document file, and I need to be able to approximate the number of pages consumed by this document file, I also need to be able to get the height of individual components (like a single paragraph or a table) to have the data I need to rearrange so I can make the document have less pages.

I don’t have a hard constraint on the programming language of the tool either (Python preferred), I prefer not embedding LibreOffice into my system.

Also I’m willing to hear other solutions (maybe my input is not the optimal thing I can use for this problem).

Thanks in advance!

  • Red1C3@lemmy.worldOP
    link
    fedilink
    English
    arrow-up
    0
    ·
    8 months ago

    Yeah my main is issue is trying to figure out how many pages it spans, I’ve looked at some docx and odt libs, none did seem to have an API related to getting the number of pages nor the height of some component (except for stuff with fixed heights like images…).

    The underlying issue is that I want to create an exam paper with the least papers possible per exam, so I guess that at least I should be able to get the height of each question of the exam and rearrange them (using an algorithm) in a fashion that uses less papers.

    • Turun@feddit.de
      link
      fedilink
      arrow-up
      0
      ·
      edit-2
      8 months ago

      How about generating latex source code, compiling it and getting the page count of the generated PDF? Reorder your set of questions and see if the result is better or worse. Optionally do it in a smart way to reduce the number of PDF compilations you have to do. (Simulated annealing comes to mind for example.)

      I think it would be easier to find a library to find the last line on a PDF page than it is to parse unzipped odt files and basically write a layout engine that does the same as libre office just to get the number of pages.

      Maybe you can even get Tex to put it in the log during compilation. That would be the most convenient option and seems reasonable to achieve.