omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello β€” I'm @olemoritz on Twitter.


    Export code and output as pdf file

    Pythonista
    pdfkit pdf
    5
    23
    12575
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cvp
      cvp @johnsilvabr last edited by cvp

      @johnsilvabr this little script

      • is based on an @omz sample
      • needs that you install xhtml2pdf via stash, then pip install xhtml2pdf
      • can be added as a tool via wrench menu
      • when executed on an edited script, creates a file script_name.pdf with highlighted code
      # code needs to install xhtml2pdf in site-packages
      # via stash/pip install xhtml2pdf
      import editor
      import os
      from pygments import highlight
      from pygments.lexers import PythonLexer
      from pygments.formatters import HtmlFormatter
      from pygments.styles import get_style_by_name
      from xhtml2pdf import pisa
      
      # Syntax-highlight code
      # from omz code at https://forum.omz-software.com/topic/1950/syntax-highlight-python-code-on-screen-while-running
      code = editor.get_text()
      html_formatter = HtmlFormatter(style='colorful')
      highlighted_code = highlight(code, PythonLexer(), html_formatter)
      styles = html_formatter.get_style_defs()
      html = '<html><head><style>%s</style></head><body>%s</body></html>' % (styles, highlighted_code)
      
      # html -> pdf
      path = editor.get_path()
      file_name = os.path.basename(path)
      i = file_name.rfind('.')
      if i>= 0:
      	file_name = file_name[:i] + '.pdf'
      
      with open(file_name, 'wb') as fil:
      	pisa.CreatePDF(html,fil)
      

      mikael 1 Reply Last reply Reply Quote 0
      • mikael
        mikael @cvp last edited by mikael

        @cvp, added an API call to hilite.me service to the Shortcut and got this (with line numbering included).

        cvp 1 Reply Last reply Reply Quote 0
        • cvp
          cvp @mikael last edited by cvp

          @mikael I had also thought to an online conversion html to pdf but it needs Internet and you have to share/select Shortcuts/select shortcut instead of tools/select tool
          thus one step more πŸ˜‚

          all I deduce is that we have a great application with Pythonista

          Edit: could you share the shortcut, I'm interested, thanks

          Edit2: and your solution provides lines numbers, I'm giving up, you're the best

          mikael 1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by

            Note the professor wants both code AND program output.
            (output, i can understand... i have no idea why anyone would ever want to see python code in pdf form)

            I imagine also that he might expect some kind of header/format?

            cvp 1 Reply Last reply Reply Quote 0
            • cvp
              cvp @JonB last edited by

              @JonB Agree, but passing via html helps for other infos like headers..., isn'it.

              1 Reply Last reply Reply Quote 0
              • mikael
                mikael @cvp last edited by

                @cvp, I agree that an all-Pythonista solution would be nicer and provide more opportunities to handle several files with a single click. Anyway, here’s the shortcut.

                cvp 1 Reply Last reply Reply Quote 0
                • cvp
                  cvp @mikael last edited by

                  @mikael Thanks a lot. I didn't even know this feature of posting to an API in Shortcuts app.
                  I like it.

                  I always try to process all in Pythonista but I wait the day when @omz would add the functionality to share/open a file to a specific app without passing via a menu, because Shortcuts can do it...

                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by ccc

                    path = editor.get_path()
                    file_name = os.path.basename(path)
                    i = file_name.rfind('.')
                    if i>= 0:
                        file_name = file_name[:i] + '.pdf'
                    
                    # Can be rewritten as...
                    
                    file_name = os.path.basename(os.path.splitext(editor.get_path())[0]) + '.pdf'
                    
                    cvp 1 Reply Last reply Reply Quote 0
                    • cvp
                      cvp @ccc last edited by cvp

                      @ccc Thanks. Shame on me, I knew it.... 😒

                      Edit: also modified in my github

                      1 Reply Last reply Reply Quote 0
                      • cvp
                        cvp last edited by

                        Still to find in the tool the way to get the console output to add it to the pdf...

                        mikael 1 Reply Last reply Reply Quote 0
                        • mikael
                          mikael @cvp last edited by

                          @cvp, easier to redirect all output to a file?

                          cvp 1 Reply Last reply Reply Quote 0
                          • cvp
                            cvp @mikael last edited by

                            @mikael Sure but needs to change the code?

                            mikael 1 Reply Last reply Reply Quote 0
                            • cvp
                              cvp last edited by cvp

                              This code should return the console content

                              from objc_util import *
                              
                              def GetConsoleText():
                              	print('called')
                              	win = ObjCClass('UIApplication').sharedApplication().keyWindow()
                              	main_view = win.rootViewController().view()	
                              	ret = '' 
                              	def analyze(v):
                              		ret = None
                              		for sv in v.subviews():
                              			if 'textview' in str(sv._get_objc_classname()).lower():
                              				if 'OMTextEditorView' not in str(sv.superview()._get_objc_classname()):	# not TextView of script
                              					return sv.text()
                              			ret = analyze(sv)
                              			if ret:
                              				return ret
                              	ret = analyze(main_view)
                              	return ret
                              

                              Thus full code can be found here

                              1 Reply Last reply Reply Quote 0
                              • mikael
                                mikael @cvp last edited by

                                @cvp, no need to change the code:

                                import contextlib, runpy
                                
                                with open('ex1_result.txt', 'w') as f:
                                  with contextlib.redirect_stdout(f):
                                    runpy.run_module('ex1')
                                
                                cvp 1 Reply Last reply Reply Quote 0
                                • cvp
                                  cvp @mikael last edited by

                                  @mikael I didn't know this functionality.
                                  But, that will say that the whole console is redirected, even if your script asks questions...
                                  Anyway, this is a shorter and general solution.

                                  mikael 1 Reply Last reply Reply Quote 0
                                  • mikael
                                    mikael @cvp last edited by

                                    @cvp, agree, case by case either option may be more applicable. I wanted to remind us of both of these capabilities (running a py file and redirecting output), as both might come handy in various contexts.

                                    cvp 1 Reply Last reply Reply Quote 0
                                    • cvp
                                      cvp @mikael last edited by

                                      @mikael "My" solution is not very elegant and I'm not sure it is robust and would stay operational in next versions of Pythonista.
                                      I searched this way for the fun because I I thought that if the console is displayed and is scrollable, it has to be in a TextView...
                                      And during my tests, I discovered the "console πŸ”½ " button and its pop up menu where you can copy all the console...In the past, I often selected the whole text and copied without knowing this possibility 😒

                                      1 Reply Last reply Reply Quote 1
                                      • First post
                                        Last post
                                      Powered by NodeBB Forums | Contributors