View Single Post
 
Old 06-08-2021, 11:21 PM
CaptainCsaba CaptainCsaba is offline Windows 10 Office 2016
Novice
 
Join Date: Jun 2021
Posts: 4
CaptainCsaba is on a distinguished road
Default

Quote:
Originally Posted by Charles Kenyon View Post
Try opening directly in Word and using Word to convert one.
Unfortunately it has the same result.

Quote:
Originally Posted by Guessed View Post
I don't know how to convert VBA code to Python but this should give you an idea on how you could copy the headers and footers into the body of the document.
Code:
Sub HFExtractor()
  Dim aSect As Section, aHF As HeaderFooter, aRng As Range
  For Each aSect In ActiveDocument.Sections
    For Each aHF In aSect.Headers
      Set aRng = aSect.Range
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.FormattedText = aHF.Range.FormattedText
    Next aHF
    For Each aHF In aSect.Footers
      Set aRng = aSect.Range
      aRng.Collapse Direction:=wdCollapseEnd
      aRng.FormattedText = aHF.Range.FormattedText
    Next aHF
  Next aSect
End Sub
I can call VBA codes from Python so they are definitely useful. I ran your code and it does extract headers just as needed. Only problem is that they all get added in bulk at the beginning of the next section. I need to retain their original position as much as possible (at least so they come after the text the come after in the PDF).

Might be a stupid idea as I am unfamiliar with how Word works, but can we for example get the x,y coordinates of these paragraphs and insert text "above them". Or something similar so we can keep the position as closely as possible?
Reply With Quote