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

      @johnsilvabr Perhaps this?

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

        @cvp, dependecy on PyQt seems problematic? Looks like @ccc could comment, listed as contributor.

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

          @mikael You're right, I'll search another way, that should be easy, I hope

          starting from this

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

            @cvp, but aren’t we making this unnecessarily difficult? In Apple Shortcuts, there is a Create PDF action, and the Gallery includes a ready-made script for that (select the margins switch to make it look nicer).

            When viewing the code in Pythonista, select wrench, Share, Shortcuts and pick this Shortcut - done.

            Something fancy could be created to automate this for a large number of files and their outputs, but for the OP issue, probably enough?

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

              @mikael Sorry, which shortcut? If it exists 😂,I agree with you. I often write a little script for the fun and that allows me to postpone my (too) big projects

              Edit: bad read of your post, I think there was an Apple standard shortcut, sorry, one more time

              Edit2: very good but I wanted to get the syntax highlighting like in editor

              1 Reply Last reply Reply Quote 0
              • 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
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors