Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

automate ms word with python

from docx import Document
from docx.shared import Inches
import pandas as pd
import win32com.client




def make_client_invoice(name, email, product, unit, price):
    document = Document()
    document.add_picture('brand_logo.png', width=Inches(1))
    document.add_heading('Invoice', 0)
    p1 = document.add_paragraph('Dear ')
    p1.add_run(name).bold=True
    p1.add_run(',')

    p2 = document.add_paragraph('Please find attached invoice for your recent purchase of ')
    p2.add_run(str(unit)).bold = True
    p2.add_run(' units of ')
    p2.add_run(product).bold=True
    p2.add_run('.')

    [document.add_paragraph('') for _ in range(2)]
    
    table = document.add_table(rows=1, cols=4)
    hdr_cells = table.rows[0].cells
    hdr_cells[0].text = 'Product Name'
    hdr_cells[1].text = 'Units'
    hdr_cells[2].text = 'Unit Price'
    hdr_cells[3].text = 'Total Price'
    for i in range(4):
        hdr_cells[i].paragraphs[0].runs[0].font.bold = True
        
    row_cells = table.add_row().cells
    row_cells[0].text = product
    row_cells[1].text = f'{unit:,.2f}'
    row_cells[2].text = f'{price:,.2f}'
    row_cells[3].text = f'{unit * price:,.2f}'
    
    [document.add_paragraph('') for _ in range(10)]

    document.add_paragraph('We appreciate your business and and please come again!')
    document.add_paragraph('Sincerely')
    document.add_paragraph('Jay')

    document.save(f'{name}.docx')

def docx_to_pdf(src, dst):
    word = win32com.client.Dispatch("Word.Application")
    wdFormatPDF = 17
    doc = word.Documents.Open(src)
    doc.SaveAs(dst, FileFormat=wdFormatPDF)
    doc.Close()
    word.Quit()

def send_email(name, to_addr, attachment):
    outlook = win32com.client.Dispatch("Outlook.Application")
    mail = outlook.CreateItem(0)
    mail.To = to_addr #'amznbotnotification@gmail.com'
    mail.Subject = 'Invoice from PythonInOffice'
    mail.Body = f'Dear {name}, Please find attached invoice'
    mail.Attachments.Add(attachment)
    mail.Send()
Comment

PREVIOUS NEXT
Code Example
Python :: python certificate verify failed unable to get local issuer certificate nltk 
Python :: convert float array to integer 
Python :: How to use glob.escape() function in python 
Python :: python is x string methods 
Python :: godot ternary 
Python :: attach short list to pandas dataframe with filler 
Python :: apply WEKA filter on customer dataset 
Python :: Python NumPy atleast_3d Function Example 
Python :: Python NumPy moveaxis function Example 02 
Python :: how to murj record in django 
Python :: python access to vraiable in another classe 
Python :: Python NumPy asarray Function Example Tuple to an array 
Python :: Python NumPy concatenate Function Example when axis equal to 0 
Python :: how to kill python program 
Python :: Python NumPy dsplit Function Syntax 
Python :: maximaze window in tkinter 
Python :: python interpreter after running a python file 
Python :: Open S3 object as string in Python 3 
Python :: NumPy bitwise_or Code When inputs are Boolean 
Python :: tikzplotlib set figure 
Python :: enumerate and looping backward 
Python :: tkintre sub windows 
Python :: python special methods list 
Python :: list x[:-1] 
Python :: pixel accuracy image segmentation python 
Python :: python if corto 
Python :: Dynamic use of templates in Jinja2 
Python :: cyclic rotation python 
Python :: clear notebook output 
Python :: negative max in python 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =