0

I'm starting to use python now and I want to automatize one process on some engineering reports I have to do. To do the reports I use MSWord and Excel, using Excel as database and copying manually the graphics and tables to Word to generate the actual report file.

Using pyautogui I've already been able to locate the areas I want to copy, and copy them from Excel. My problem now is that I don't know a way I can make python alternate from Excel to Word and paste, then go back to the Excel file and copy the new info, go back to Word and paste, etc until all infos are compiled so I can save a .pdf file.

Is there any other library I should be using or a way to do it on pyautogui?

  • But can't you just export excel into pdf? – Alexey S. Larionov Jul 01 '21 at 16:48
  • 3
    Yes we have libraries, for that. but the approach you are applying is a kids way of doing that. A better way would be, use file handling libraries, like we have libraries that directly play with excel and word files. also some libraries that deals with pdfs. – Hari Kishore Jul 01 '21 at 16:48
  • broadly: you could skip using the Microsoft tools and directly create the final document with Python, but this would take a process flow change. As-is, this is a common [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) over the assumption that you must use some awkward document authoring tool – ti7 Jul 01 '21 at 16:48
  • https://stackoverflow.com/questions/15422274/switching-windows-with-python – ti7 Jul 01 '21 at 16:49
  • but if you want to switch between windows, definately there are some library for that as well. in its very basic term you can use even pyautogui as well for that purpose. – Hari Kishore Jul 01 '21 at 16:51
  • Is OLE still a thing? I would have thought that was the way to directly link charts and graphs generated in Excel into a Word document. – chepner Jul 01 '21 at 17:00
  • I'm gonna look for the file handling libraries. OLE works well on graphs and charts, but on the tables I have to show on the report it didn't work very well. – Murilo Dantas Campos Jul 01 '21 at 17:15

1 Answers1

0

I suggest using something like openpyxl to open the Excel file directly rather than trying to manipulate it through the Excel UI. Similary, use a library like pyPDF or something similar to write directly to a PDF file.

If you insist on manipulating Excel and Word directly. You don't have to switch back and forth. First get all the data you need from Excel and store it somewhere. Then open Word and do what you need to with the stored data.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268