python generate word document

Solutions on MaxInterview for python generate word document by the best coders in the world

showing results for - "python generate word document"
Adame
26 Oct 2018
1from __future__ import print_function
2from mailmerge import MailMerge
3from datetime import date
4
5# Define the templates - assumes they are in the same directory as the code
6template_1 = "Practical-Business-Python.docx"
7template_2 = "Practical-Business-Python-History.docx"
8
9# Show a simple example
10document_1 = MailMerge(template_1)
11print("Fields included in {}: {}".format(template_1,
12                                         document_1.get_merge_fields()))
13
14# Merge in the values
15document_1.merge(
16    status='Gold',
17    city='Springfield',
18    phone_number='800-555-5555',
19    Business='Cool Shoes',
20    zip='55555',
21    purchases='$500,000',
22    shipping_limit='$500',
23    state='MO',
24    address='1234 Main Street',
25    date='{:%d-%b-%Y}'.format(date.today()),
26    discount='5%',
27    recipient='Mr. Jones')
28
29# Save the document as example 1
30document_1.write('example1.docx')
31
32# Try example number two where we create multiple pages
33# Define a dictionary for 3 customers
34cust_1 = {
35    'status': 'Gold',
36    'city': 'Springfield',
37    'phone_number': '800-555-5555',
38    'Business': 'Cool Shoes',
39    'zip': '55555',
40    'purchases': '$500,000',
41    'shipping_limit': '$500',
42    'state': 'MO',
43    'address': '1234 Main Street',
44    'date': '{:%d-%b-%Y}'.format(date.today()),
45    'discount': '5%',
46    'recipient': 'Mr. Jones'
47}
48
49cust_2 = {
50    'status': 'Silver',
51    'city': 'Columbus',
52    'phone_number': '800-555-5551',
53    'Business': 'Fancy Pants',
54    'zip': '55551',
55    'purchases': '$250,000',
56    'shipping_limit': '$2000',
57    'state': 'OH',
58    'address': '1234 Elm St',
59    'date': '{:%d-%b-%Y}'.format(date.today()),
60    'discount': '2%',
61    'recipient': 'Mrs. Smith'
62}
63
64cust_3 = {
65    'status': 'Bronze',
66    'city': 'Franklin',
67    'phone_number': '800-555-5511',
68    'Business': 'Tango Tops',
69    'zip': '55511',
70    'purchases': '$100,000',
71    'shipping_limit': '$2500',
72    'state': 'KY',
73    'address': '1234 Adams St',
74    'date': '{:%d-%b-%Y}'.format(date.today()),
75    'discount': '2%',
76    'recipient': 'Mr. Lincoln'
77}
78
79document_2 = MailMerge(template_1)
80document_2.merge_pages([cust_1, cust_2, cust_3])
81document_2.write('example2.docx')
82
83# Final Example includes a table with the sales history
84
85sales_history = [{
86    'prod_desc': 'Red Shoes',
87    'price': '$10.00',
88    'quantity': '2500',
89    'total_purchases': '$25,000.00'
90}, {
91    'prod_desc': 'Green Shirt',
92    'price': '$20.00',
93    'quantity': '10000',
94    'total_purchases': '$200,000.00'
95}, {
96    'prod_desc': 'Purple belt',
97    'price': '$5.00',
98    'quantity': '5000',
99    'total_purchases': '$25,000.00'
100}]
101
102document_3 = MailMerge(template_2)
103document_3.merge(**cust_2)
104document_3.merge_rows('prod_desc', sales_history)
105document_3.write('example3.docx')
106