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
CodePudding user response:
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')
CodePudding user response:
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.