Inkscape.org
Creating New Extensions multipage in Inkscape 1.2
  1. #1
    Jurgen Gaeremyn Jurgen Gaeremyn @JurgenG

    Hi,

    will inkex support to add pages programmatically?

    What would be the best way to add the <inkscape:page ... >element to the file?

  2. #2
    Jurgen Gaeremyn Jurgen Gaeremyn @JurgenG
    *

    I think the easiest approach would be to use the get_or_create() command, but I'm getting an error message. This is what I'm trying:

    self.svg.get_or_create("//inkscape:page")

    (the following is the error message I get when trying to perform the command in the terminal:

    svg.get_or_create(xpath="\\inkscape:page", nodeclass=None)

    This results in the following error:

    <input>:1: DeprecationWarning: invalid escape sequence \p
    <input>:1: DeprecationWarning: invalid escape sequence \p
    Traceback (most recent call last):
      File "/usr/lib/python3.9/code.py", line 90, in runcode
        exec(code, self.locals)
      File "<input>", line 1, in <module>
      File "/home/jurgen/.config/inkscape/extensions/lib/python3.9/site-packages/inkex/elements/_base.py", line 282, in get_or_create
        node = self.findone(xpath)
      File "/home/jurgen/.config/inkscape/extensions/lib/python3.9/site-packages/inkex/elements/_base.py", line 344, in findone
        el_list = self.xpath(xpath)
      File "/home/jurgen/.config/inkscape/extensions/lib/python3.9/site-packages/inkex/elements/_base.py", line 336, in xpath
        return super(BaseElement, self).xpath(pattern, namespaces=namespaces)
      File "src/lxml/etree.pyx", line 1597, in lxml.etree._Element.xpath
      File "src/lxml/xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
      File "src/lxml/xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
    lxml.etree.XPathEvalError: Invalid expression

    Can someone point me in the right direction?

    In essence, I want to inject these blocks for every page into the main svg element:

        <inkscape:page
           x="0"
           y="0"
           width="210"
           height="297"
           id="page2306" />

    If I understand it, my xpath expression is not right. That makes perfect sense as I don't know how to write the xpath for a newly to be injected element.

  3. #3
    Jurgen Gaeremyn Jurgen Gaeremyn @JurgenG
    *

    Nevermind... it had to be forward slash :p

    This actually found the (already existing) pages... didn't get me to actually create a page though.

    But this took me to my next issue...

    This is my next try:

    svg.get_or_create(xpath="//inkscape:page[@id='test']", nodeclass=None)

    Now it seems to want to create the element, since it didn't find it. But now the "nodeclass=None" isn't accepted.

    Traceback (most recent call last):
      File "/usr/lib/python3.9/code.py", line 90, in runcode
        exec(code, self.locals)
      File "<input>", line 1, in <module>
      File "/home/jurgen/.config/inkscape/extensions/lib/python3.9/site-packages/inkex/elements/_base.py", line 284, in get_or_create
        node = nodeclass()
    TypeError: 'NoneType' object is not callable

     

  4. #4
    Jurgen Gaeremyn Jurgen Gaeremyn @JurgenG
    *

    For future reference: multipage support is added to Inkex. No more need to manually fiddle with XML.

    These are some example commands:

    To add a page:

    self.svg.namedview.add(Page(width="210", height="297", x="0", y="0"))
    self.svg.namedview.new_page(x="220", y="0", width="147.5", height="210", label="TEST")
    
    To iterate through pages and get info:
    for i, page in enumerate(self.svg.namedview.get_pages()):
        self.msg("Page number {}: x: {} y: {} width: {} height: {}".format(i+1, page.get("x"), page.get("y"), page.get("width"), page.get("height")))