Python docx module-Cover Page of the word document - python-3.x

I am working on an existing word report and trying to do some automation with python docx module. I need to get the report date from database and paste it to "cover page" of the doc but I couldn't find any attribute about cover page in module. How can I do it?

What you can do is:
In the word document write any text where you want to replace the date picked from your database eg: dd-mm-yyyy
You can now search for your entered text "dd-mm-yy" in the word file using regular expressions and replace it with the Date you got from your database.
The code will be as follows:
def docx_replace_regex(doc_obj,replaceDate):
regex = re.compile(r"dd-mm-yyyy")
for p in doc_obj.paragraphs:
if regex.search(p.text):
p.text = regex.sub(replaceDate, p.text)
doc.save('generatedDocument.docx')
filename = "Your Word Document Path.docx"
doc = Document(filename)
docx_replace_regex(doc,date)

Related

Find and replace header/footer text in Word with pywin32

I hope everyone reading this is well. My problem is as follows:
I am trying to find a way to find and replace text in the header (or footer) of a Word document (docx), using the pywin32 library.
Here's what I've tried...
I've only made it as far as replacing the header (or footer) text in its entirety. Here's the code for that.
import win32com.client
word_app = win32com.client.DispatchEx('Word.Application')
word_app.Visible = False
word_app.DisplayAlerts = False
word_app.Documents.Open(str('sourcefile.docx'))
'''
For this example, sourcefile.docx has three pages; and it is
configured to have a different header for the first page,
hence Headers(2) and Headers(1) below.
'''
word_app.ActiveDocument.Sections(1).Headers(2).Range.Text = "Page 1 header text"
word_app.ActiveDocument.Sections(1).Headers(1).Range.Text = "Subsequent pages header text"
word_app.ActiveDocument.SaveAs(str(outputfile))
word_app.ActiveDocument.Close(SaveChanges=False)
word_app.Application.Quit()
What I want to do is have templated text in the header; e.g., %NAME% Agreement, and replace only the variable portions. In this example, it's only the %NAME% part. At present, I have only managed to accomplish a total replacement of the entirety of the header's content. I'd like to be more surgical. Any help is greatly appreciated.

How to read bookmarks that are outside of a paragraph with Apache POI XWPF

Sometimes, when defining a bookmark in a .docx file (using Word 2016 in this case), the bookmark start tag (<w:bookmarkStart>) is placed by MS Word before the start of the paragraph. Unzipping the .docx file reveals the following:
<w:bookmarkStart w:id="38" w:name="MyBookmark"/><w:p w14:paraId="1B2F3A46" ...>...
<w:bookmarkEnd w:id="38"/></w:p>
Such a bookmark is not listed by iterating in any of the paragraphs bookmarkStart elements, so that the following code will not list this bookmark:
XWPFDocument document = ...
for (XWPFParagraph paragraph : document.getParagraphs())
{
for(CTBookmark bookmark : paragraph.getCTP().getBookmarkStartList())
System.out.println(bookmark.getName());
{
}
Question is how to discover that sort of bookmark that is placed outside of a paragraph?

NotesRichTexItem : Insert text strings at the first position in existing rich text data

I'd like to insert the text string into the existing rich text field data at the first position for all of documents in a DB.
NotesRichTextNavigator.FindFirstElement method - This method needs to specify the element type to search but I simply insert the text at the first position of the rich text data.
This might be very basic question, but I could not find the way and waste a few hours... Please help me!
You can do this using a workaround. Instead of working with FindFirstElement, you create a dummy richtextitem, containing the text that you need to prepend to your original item,
add the original item to the dummy item, delete the original item and recreate it.
Then add the dummy item and delete the dummy.
This sounds complex, but it is not that hard actually. Here's a small example in LotusScript on how to do this on a document:
'Get your richtext field
Set rtf = doc.getfirstItem("myRTF")
'create the dummy
Set rtDummy = doc.Createrichtextitem("rtfDummy")
'set the text that you want to insert in your richtext field
Call rtDummy.appendText("Inserting a line of text at the top")
'Add a line to make sure the inserted text is on a separate paragraph
Call rtDummy.Addnewline(1, true)
'Add the content of the original richtext item
Call rtDummy.Appendrtitem(rtf)
'Remove the original item and recreate it
Call rtf.Remove()
Set rtf = doc.Createrichtextitem("myRTF")
'Append the dummy item (including the added text)
Call rtf.Appendrtitem(rtDummy)
'Remove the dummy item
Call rtDummy.Remove()
'Save the document
Call doc.Save(True, True)

How to get data from Excel and merge it into Word using MailMerge?

Trying to get data from Excel and merge it into Word using MailMerge (just like how it is done in this video).
However, fields aren't getting updated after running this code. VBA isn't throwing any error so looks like code is fine. Can you please help?
Sub getdata()
Dim numRecord As Integer
Dim myName As String
myName = InputBox("Enter the field name and relax!")
Set dsMain = ActiveDocument.MailMerge.DataSource
If dsMain.FindRecord(FindText:=myName, Field:="Fields") = True Then
numRecord = dsMain.ActiveRecord
End If
End Sub
Note: Data in Excel looks like this:
Fields First Layer Second Layer
CC 5 3
So when someone enters CC in Input box I want first_layer and Second_layer fields in word to get updated to 5 and 3 respectiely.
If you're running the mailmerge from Word, you don't actually need any VBA for this - it can all be done with a SKIPIF field. For example the following field code does the same as the macro in the video is supposed to:
{SKIPIF{FILLIN "Name to merge" \o}<> {MERGEFIELD Name}}
or:
{SKIPIF{FILLIN "Name to merge" \o}<> «Name»}
Note: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. Likewise, the chevrons (i.e. '« »') are part of the actual mergefields - which you can insert from the 'Insert Merge Field' dropdown (i.e. you can't type or copy & paste them from this message, either). The spaces represented in the field constructions are all required.

Copy Excel tables to Outlook email with Python

I'm using pywin32 with python3 and I'm trying to copy some pivot tables into a formatted email in Outlook. I have the Excel side done, but not sure on the Outlook side. I can paste tables into the email, but I'd like to have formatted text before and after them. I was thinking of possibly storing a template-type thing in a Word document, but not sure what to do
I have something like
from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
xlsx = xl.Workbooks.Open('doc.xlsx')
xlsx.Sheets.Item('Some Sheet').PivotTables('PivotTable 1').TableRange2.Copy()
word = Dispatch('Word.Application')
docx = word.Documents.Open('doc.docx')
docx.Bookmarks('PivotTable1').Range.PasteExcelTable(False, False, False)
docx.Content.Copy()
outlook = Dispatch('Outlook.Application')
mail = outlook.CreateItem(0)
mail.BodyFormat = 3
we = mail.GetInspector.WordEditor
we.Range().Paste()
Edit: Figured out a way to get it to work using an intermediary Word document as a template with Bookmarks as locations to insert things. Open to suggestions of better ways to do this, though.

Resources