Inkscape.org
Creating New Extensions how to make a txt file
  1. #1
    JorgeInkscape JorgeInkscape @JorgeInkscape
    Hello group, I want to create a txt file with inkscape but I can't do it.
    who can help me?

    ------------------------------------------------ prueba.py ----------------------------------

    import inkex

    class Greet(inkex.GenerateExtension):
        def run(self):
            file = open("C:/datos.txt", "w")
            file.write("Hola mundo")
            file.close()

    ------------------------------------------------ prueba.ink ----------------------------------

    <?xml version="1.0" encoding="UTF-8"?>
    <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension">
      <name>Prueba</name>
      <id>org.inkscape.tutorial.prueba</id>
      <effect>
        <effects-menu>
          <submenu name="Propios"/>
        </effects-menu>
      </effect>
      <script>
        <command location="inx" interpreter="python">myExtension/prueba.py</command>
      </script>
    </inkscape-extension>
     

  2. #2
    inklinea inklinea @inklinea⛰️

    import inkex

    class Greet(inkex.GenerateExtension):

        def generate(self):
            file = open("C:/Users/xps/datos.txt", "w")
            file.write("Hola mundo")
            file.close()

    if __name__ == '__main__':
        Greet().run()
        
    This will work.

    However, the 'GenerateExtension' is used for a specific purpose (to return svg fragments)

    Unless you need to I would just use a normal 'EffectExtension'

    Normally, when writing to files you would use pythons 'with' statement to automatically close the file after writing.

    -- Unless you want to use the file elsewhere in the code before closing.

    I would make a small function with a try / except statement.

    That way you can protect against invalid filepaths

    import inkex

    def save_text_to_file(self, string, filepath):

        try:
            with open(filepath, 'w') as my_file:
                my_file.write(string)
        except:
            inkex.errormsg(f'Cannot Write Text File {filepath}')


    class WriteTextFile(inkex.EffectExtension):

        def add_arguments(self, pars):
            pass
        
        def effect(self):

            my_text = """Whether you are an illustrator, designer, web designer or just someone who needs to create some vector imagery, Inkscape is for you!

    Flexible drawing tools
    Broad file format compatibility
    Powerful text tool
    Bezier and spiro curves        
            """

            save_text_to_file(self, my_text, '/home/name/Documents/foo.txt')
            
    if __name__ == '__main__':
        WriteTextFile().run()

  3. #3
    JorgeInkscape JorgeInkscape @JorgeInkscape

    Hi inklinea.

    Many thanks. I added the lines of code you showed me and ran Inkscape in administrator mode and it can get the file. I am a beginner and I am doing some tests.

    Many thanks

  4. #4
    JorgeInkscape JorgeInkscape @JorgeInkscape

    Hi inklinea.

    Thanks to your help, I was able to create this code that saves the ID of the objects selected in Inkscape in a text file.

    -------------------------------------------------- CODE-------------------------------------------------------------------

    import inkex
    
    class GetID(inkex.EffectExtension):
        def add_arguments(self, pars):
            pass # We don't need arguments for this extension
    
        def effect(self):
            # Crear una lista vacía para los ID de los objetos seleccionados
            ids = []
    
            for elem in self.svg.selection:
                ids.append(elem.get("id"))
    
            with open("C:/id_objects.txt", "w") as file:
                file.write(" ".join(ids))
            
    if __name__ == '__main__':
        GetID().run()
    
  5. #5
    inklinea inklinea @inklinea⛰️

    Do not run Inkscape in Administrator mode.

    You could quite easily make a mistake in Python and delete your hard drive !

    Writing anything to C:\ which is the root of the operating system is something which is unusual and basically never done. 

    That is why you had to use admin rights to do it. 

    Is there are reason you want to save in C:\ and not to your user folder for example ? 

     

  6. #6
    JorgeInkscape JorgeInkscape @JorgeInkscape

    Hi, inklinea

    There is no reason, it seemed faster to test the code. Thanks for your recommendation, I already changed the path for the file