1

I wish to make some code to edit the date on a pdf form. (the goal is to make all the forms for a whole year at once) The PDF form is not normally editable, but with LibreOffice draw it can be easily edited. The problem is with LibreOffice Draw that it is not possible to record Macros, and the language is horribly complicated to me.

I would like to use python or something, but I have no idea how to start. One option would be to just make everything controlled by mouseclicks and keyboard commands, this would not be a problem for me to make. But I imagine that there can be more elegant solutions, to interact directly with the file.

Any tips?

Update: Here is a link to a pdf file with only the relevant textbox copied directly from the PDF file I want to edit by code: https://ufile.io/gw9e13er

Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73

2 Answers2

0

One possible solution is using the pyautogui library in Python.

Even though it is absolutely not the most elegant solution you could just quickly hardcode it like this:

import pyautogui as auto

auto.click(X, Y) # click into your textfield
auto.write("Hello World!") # write into your textfield

While you can get your relevant coordinates by looping over

print(auto.position())

If you need to press multiple buttons at the same time, e.g. ctrl + c, alt + tab you can do so by writing

auto.hotkey('ctrl', 'c')
auto.hotkey('alt', 'tab')
Alexander
  • 35
  • 7
  • Thank you, this solution I have considered, didn't look in to the relevant commands yet so thanks for sharing. It would be nice to do it in a more elegant way, especially when it is for multiple people, that I can easily make and send them a zip file. BUT, of course letting the computer run for 10 minutes or more making many files is definitely an option. The reason to know about the macros would be that I imagine it to be a useful skill that could come in handy in the future; to have efficient automatized filling of forms. – Willem van Houten Apr 11 '22 at 10:46
0

Here is some example Python-UNO code.

def changeDrawText():
    oDoc = XSCRIPTCONTEXT.getDocument()
    oDrawPage = oDoc.getDrawPages().getByIndex(0)
    oShape = oDrawPage.getByIndex(0)
    oText = oShape
    oCurs = oText.createTextCursorByRange(oText.getStart())
    oCurs.goRight(6, False)
    oText.insertString(oCurs, "example text", False)

If you have not worked with LibreOffice UNO before, be sure to check out https://www.pitonyak.org/oo.php.

Jim K
  • 12,824
  • 2
  • 22
  • 51