Autofill a word.docm file using python - python-3.x

I am trying to auto-fill a word.docm file using python.
I could find solution to automate word.docx filling. But with word.docm files, the code fails.
This is how the sample 'template.docm' file looks like.
I need to fill field1 and field2 from python.
I am attaching the code,that worked perfectly for docx files. Can anyone please suggest any edits or any other method that works for word.docm files using python?
Thanks in advance
from __future__ import print_function
from mailmerge import MailMerge
from datetime import date
template = "template.docm"
document = MailMerge(template)
print(document.get_merge_fields())
document.merge(field1='Name',field2='Address')
document.write('template-output.docm')

Related

xlwings set_mock_caller() return None

Windows 10, Python 3.6, xlwings 0.27.8
When trying to debug my code outside of RunPython, I keep stumbling on the following issue, for example:
import xlwings as xw
xlsx_file = 'anExcelFile.xlsm'
xlwx = xw.book(xlsx_file).set_mock_caller()
From there, I am hoping to be able to use xlsw as normally as such if I had used the routine from RunPython, but now, typing xlsw returns None
However, if i do:
xlsx = xw.book(xlsx_file).set_mock_caller
then xlsx contains: <Book ['anExcelFile.xlsm]>
but still, xlsx() returns None.
Any lead on what I am getting wrong would help, thanks!
Finally understood my issue.
As explained in xlwings, the code sequence should read as follows:
import xlwings as xw
xlsx_file = 'anExcelFile.xlsm'
xw.book(xlsx_file).set_mock_caller()
# Sets the Excel file which is used to mock xw.Book.caller()
# when the code is called from Python and not from Excel via RunPython.
# and then:
xlsx = xw.Book.caller()
Now, xlsx returns: <Book [anExcelFile.xlsm]>

Write output in xlsb file format (Excel binary file format) using pandas and pyxlsb

I've read a lot of stackoverflow and other threads where it's been mentioned how to read excel binary file.
Reference: Read XLSB File in Pandas Python
import pandas as pd
df = pd.read_excel('path_to_file.xlsb', engine='pyxlsb')
However, I can not find any solution on how to write it back as .xlsb file after processing using pandas? Can anyone please suggest a workable solution for this using python?
Any help is much appreciated!
I haven't been able to find any solution to write into xlsb files or create xlsb files using python.
But maybe one work around is to save your file as xlsx using any of the many available libraries to do that (such as pandas, xlsxwriter, openpyxl) and then converting that file into a xlsb using xlsb-converter. https://github.com/gibz104/xlsb-converter
CAUTION: This repository uses WIN32COM, which is why this script only supports Windows
you can read binary file with open_workbook under pyxlsb. Please find below the code:
import pandas as pd
from pyxlsb import open_workbook
path=r'D:\path_to_file.xlsb'
df2=[]
with open_workbook(path) as wb:
with wb.get_sheet(1) as sheet:
for row in sheet.rows():
df2.append([item.v for item in row])
data= pd.DataFrame(df2[1:], columns=df2[0])

Query regarding pandas

Even after installing xlrd module, I am not able to read excel files using pandas, every time it's showing file directory not found. Please help!
I am using " import Pandas as pd"
" data=pd.read_excel("notebook.xlsx")
It shows error as file not found
Pandas is not finding the excel file. Try to put the complete path on the read_excel function like read_excel("C:/documents/notebook.xlsx").

How do I use python to show data in excel?

I'm trying to display data from Python in Excel. Ideally, a pandas dataframe's worth of data would appear in a new, unsaved excel instance. My search has turned up ways to create excel files, and lots of ways to 'open' an excel file to read data from it, but no way to display it. My current approach was to create a file and then figure out how to open it, but I consider that approach second-best.
I found this. Another way would be to export your data to CSV and import it in Excel.
I found you can 'run' files with the OS library. As long as your computer knows what to do with it, you can create the xlsx file with whatever method and then run it to display:
import xlsxwriter
import os
w = xlsxwriter.Workbook(r"C:\Temp\test.xlsx")
s=w.add_worksheet("Sheet1")
s.write(1,3,"7")
w.close()
os.startfile(r"C:\Temp\test.xlsx")
Still not sure if you can work with an unsaved open instance of excel

Importing only a specific part of the docx in Python

I am trying to extract the majority of my docx file when I am importing it to the Python. The best would be if I could tell my code which paragraphs I need or what part of the text I am going to use.
Can anyone help me with that?
I have tried this code:
import docx
doc = docx.Document('A.docx')
print(len(doc.paragraphs))
print (doc.paragraphs[2].text)
but the problem with this is that whenever I hit enter it thinks that a new paragraph has started.

Resources