Jump to content

Recommended Posts

..so I got Notepad++ but was not working with the xml printing plugin, the online beautifiers would no work all the time, and I wanted like Tidy does with tabs so it looks nice in Scite to look at the code and make my own template for a script I'm working on....very frustrating and time consuming, so, I put this code together. I get in a 10th of a sec. the xml prettified vs. minutes and frustration.

Anyway, how I use it is I drag and drop the word doc. ( saved in XML format ) to scite , copy to the clipboard ( ctrl-A, ctrl-C ) , switch to this code, press F5 and I'm happy

If Not StringInStr($CmdLineRaw, "/ErrorStdOut") And Not @Compiled Then Exit MsgBox( 262144 , @ScriptName, "please run from Editor", 10)

Local $s = ClipGet()
If Not StringInStr($s, '<?mso-application progid="Word.Document"?>') Then Exit MsgBox(262144, StringTrimRight(@ScriptName, 4), "tested only in Word.Document XML" & @CR & @CR & "no changes made to clipboard", 20)
Local $sOut = msWordXML_Beautify($s, 2) ; 2=return as beautified string, 1=ConsoleWrite beautified string, 0=return beautified array
ClipPut($sOut)
MsgBox(262144, StringTrimRight(@ScriptName, 4), "clipboard content replaced by beautified XML", 2)


Func msWordXML_Beautify($s, $iEcho = 0)
    Local $iTimer = TimerInit()
    $s = StringReplace($s, @CR, '')
    $s = StringReplace($s, @LF, '')
    Local $a = StringSplit($s, "<")
    Local $b[$a[0] * 2]
    Local $i = 0, $c = ""
    For $x = 1 To $a[0]
        If StringReplace($a[$x], @TAB, "") = "" Then ContinueLoop
        If StringInStr($a[$x], ">") Then $a[$x] = StringReplace($a[$x], @TAB, '')
        $c = StringSplit($a[$x], ">")
        If UBound($c) < 2 Then ContinueLoop
        For $y = 1 To $c[0]
            If $y = 1 Then
                $i += 1
                $b[$i] = "<" & $c[$y] & ">"
            Else
                If $c[$y] = "" Then ContinueLoop
                $i += 1
                $b[$i] = $c[$y]
            EndIf
        Next
    Next
    ReDim $b[$i + 1]
    $b[0] = $i
    For $x = 3 To $b[0]
        If Not StringInStr($b[$x - 1], ">") Then
            $b[$x] = $b[$x - 2] & $b[$x - 1] & $b[$x]
            $b[$x - 2] = "<>"
            $b[$x - 1] = "<>"
        EndIf
    Next
    Dim $c[$b[0] + 1]
    $i = 0
    For $x = 1 To $b[0]
        If $b[$x] = "<>" Then ContinueLoop
        $i += 1
        $c[$i] = $b[$x]
    Next
    $b = $c
    $c = ""
    ReDim $b[$i + 1]
    $b[0] = UBound($b) - 1
    Local $tabs = ""
    For $x = 1 To $b[0]
        $b[$x] = StringStripWS($b[$x], 3)
        If StringLeft($b[$x], 2) = "<!" Then ContinueLoop
        If StringLeft($b[$x], 2) = "<?" Then ContinueLoop
        If StringLeft($b[$x], 1) = "<" And StringRight($b[$x], 2) = "/>" Then
            $b[$x] = $tabs & $b[$x]
            ContinueLoop
        EndIf
        If StringLeft($b[$x], 2) = "</" And StringRight($b[$x], 1) = ">" Then
            $tabs = StringTrimRight($tabs, 1)
            $b[$x] = $tabs & $b[$x]
            ContinueLoop
        EndIf
        If StringLeft($b[$x], 1) = "<" And StringRight($b[$x], 1) = ">" And Not StringInStr($b[$x], '</') Then
            $b[$x] = $tabs & $b[$x]
            $tabs &= @TAB
            ContinueLoop
        EndIf
        $b[$x] = $tabs & $b[$x]
    Next
    ConsoleWrite('+ msWordXML_Beautify done in about ' & Round(TimerDiff($iTimer), 5) & ' mSec.' & @CRLF)
    Local $sOut = ""
    If $iEcho Then
        For $x = 1 To $b[0]
            If $iEcho = 1 Then
                ConsoleWrite( $b[$x] & @CRLF )
            Else
                $sOut &= $b[$x] & @CRLF
            EndIf
        Next
    EndIf
    If $iEcho = 2 Then Return $sOut
    Return $b
EndFunc   ;==>msWordXML_Beautify

..hope it saves time to someone.

Edit 1: it works nice with <?mso-application progid="Excel.Sheet"?>, it may just work with any XML, no clue.

Edit 2: fixed an error in the code

Edited by argumentum
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VinMe
      Dear all, 
      I am working on automation of ms word, on which my scripts runs with multiple word document at a time. But I wanted all the word to be running in the background.
      do we have any UDF available? Please help.
      BR,
      VinMe
       
    • By mLipok
      I was asking @eltorro serveral times for any support  for XML DOM wrapper (COM) - with no success  
          So I took matters into my hands ..... I want to present XMLWrapperEx.au3 - BETA Version
      Want to join to the project ?
       
      Here is some description:
      ; #INDEX# ======================================================================================================================= ; Title .........: XMLWrapperEx.au3 ; AutoIt Version : 3.3.10.2++ ; Language ......: English ; Description ...: Functions to use for reading and writing XML using msxml. ; Remarks .......: BETA Version ; Author ........: mLipok ; Version .......: "1.1.1.01" ; _XML_MiscProperty_UDFVersion() #CS This UDF is created on the basis of: https://www.autoitscript.com/forum/topic/19848-xml-dom-wrapper-com/ For this reason, I attach also the last known (to me) previous version ($_XMLUDFVER = "1.0.3.98" _XMLDomWrapper_1.0.3.98_CN.au3 ) For the same reason I continue to recognize the achievements of the work of my predecessors (they are still noted in each Function header). . . . . !!!!!!!!! This is BETA VERSION (all could be changed) !!!!!!!!! . . . WORK IN PROGRES INFORMATION: For now 2015-09-01 the descripion (Function Header) can not entirely correctly describe the function. TODO: in many places I used "TODO" as a keyword to find what should be done in future . I want to: PREVENT THIS: The unfortunate nature of both the scripts is that the func return results are strings or arrays instead of objects. .     I want to: USE THIS CONCEPT:     .   All function should use Refernce to the object as first Function parameter     .   All function should return in most cases objects. There should be separate functions to Change Object collection to array     .   All function should use COM Error Handler in local scope.     .   All function should return @error which are defined in #Region XMLWrapperEx.au3 - @ERROR Enums     .    All function should have the same naming convention     .    All variables should have the same naming convention     .    There should not to be any Global Variable - exception is $g__oXMLDOM_Events     .   It should be possible easy to use XML DOM Events     .        https://msdn.microsoft.com/en-us/library/ms764697(v=vs.85).aspx     .   It should be possible easy to Debug     .    Ultimately, you should be able to do anything with your XML without having to use your own Error Handler. #CE  
      More info inside zip archive.
      This UDF can be downloaded from here:
      REMARK:
      This UDF was formerly named:   XMLWrapperEx.au3 
       
    • By Exit
      I make plugins for WordPress and am dissatisfied with the built-in editor.
      I'm now looking for a way to format the PHP code like TIDY does in AU3-Editor.
      I've already tried some online tools but they are buggy or have too few formatting options.

      Buggy: http://www.phpformatter.com/
      Missing options: https://homepage-kosten.de/php_beautifier.php
      Here is the code to check if the beautifier is buggy:
      <?php /* Version: 2021.08.08#1 */ exit ( ' Version is: ' . chop ( substr ( file ( __FILE__ ) [ 2 ] , 9 , 13 ) ) ) ; /* Output from PHP Formatter: Format Error on line 5: parse error, unexpected '['([), expecting ')'! Output in Browser: Version is: 2021.08.08#1 */ ?> It would be great if you showed me a link to another beautifier that is not buggy and has options for compression.  
      e.g. do not put comments in a new line. 
      Or even better, a hint as to which PHP editor in WordPress meets these requirements.
      TIA Exit
    • By diff
      Hello,
       
      so I have started to learn to use the Word UDF and got issue to add my pictures after exact paragraphs. I was searching in the forum for the solution, checked with examples and still I don't understand how to add pictures after paragraph in new line.
       
      My word document has like 8 pages and for example on page I have paragraph named "My examples:" and here starts my problem.
       
      I have tried to do this:
      #include <Word.au3> Local $oWord = _Word_Create() Local $oDoc = _Word_DocOpen($Word, @ScriptDir & "\examples.docx", Default, Default, True) $oSearchRange = _Word_DocRangeSet($oDoc, -1, $wdParagraph, 0) $oRangeFound = _Word_DocFind($oDoc, "My examples:", $oSearchRange) _Word_DocPictureAdd($oDoc, @ScriptDir & "\pic1.jpg", Default, Default, $oRangeFound) And here the picture adds before the paragraph My examples: on same line and looks like
      {pic1}My Examples:
       
      What I want to see is:
      My examples:
      {PICTURE IN NEW LINE} which is pic1.jpg from my code.
       
      How I can do that? I want to add 3 pictures in a row in new line each like:
      My examples:
      {pic1.jpg}
      {pic2.jpg}
      {pic3.jpg}
       
      Hope I explained this well, but you can ask me if you need any additional information to clarify.
    • By ahha
      I'm trying to get the number of columns in a specific row in a Word table and am stuck.   I need a push.  Program below and Word file attached.
      Thanks.
      #AutoIt3Wrapper_run_debug_mode=Y ;use this to debug in console window #include <Word.au3> $oWord = _Word_Create(True, True) ;Create Word application object, make it visible, and force a new instance of Word $oDoc = _Word_DocOpen($oWord, @ScriptDir&"\ColumnTest.docx", Default, Default, True) ;Open the Word document $iTablesCount = $oDoc.Tables.Count ;get Tables count in $oDoc Pause("$iTablesCount = '" & $iTablesCount & "'") $iRowCount = $oDoc.Tables.Item(1).Rows.Count ;Table hard coded $iColCount = $oDoc.Tables.Item(1).Columns.Count Pause("Table#1 $iRowCount = '" & $iRowCount & " $iColCount = '" & $iColCount & "'") ;trying to get the number of columns in each row ;$ColCountInRow = $oDoc.Tables.Item(1).Rows(1).Columns.Count ;this fails and read somewhere to use Cells.Count $ColCountInRow = $oDoc.Tables.Item(1).Rows(1).Cells.Count ;hard code Row 1 <<<<< ERROR here ;this is the error I get ;: ==> The requested action with this object has failed.: ;$ColCountInRow = $oDoc.Tables.Item(1).Rows(1).Cells.Count ;$ColCountInRow = $oDoc.Tables.Item(1)^ ERROR Pause("Row 1 has " & $ColCountInRow & " Columns") Exit Func Pause($text="") MsgBox(262144, "DEBUG", "Paused: " & $text) EndFunc  
      ColumnTest.docx
×
×
  • Create New...