-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sven-Bo
committed
Sep 2, 2021
0 parents
commit b735319
Showing
16 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import os, sys # Standard Python Libraries | ||
from docxtpl import DocxTemplate, InlineImage # pip install docxtpl | ||
from docx.shared import Cm, Inches, Mm, Emu # pip install python-docx | ||
|
||
# Change path to current working directory | ||
os.chdir(sys.path[0]) | ||
|
||
doc = DocxTemplate("Template.docx") | ||
placeholder_1 = InlineImage(doc, "Placeholders/Placeholder_1.png", Cm(5)) | ||
placeholder_2 = InlineImage(doc, "Placeholders/Placeholder_2.png", Cm(5)) | ||
context = { | ||
"name": "Sven", | ||
"placeholder_1": placeholder_1, | ||
"placeholder_2": placeholder_2, | ||
} | ||
|
||
doc.render(context) | ||
doc.save("Template_Rendered.docx") |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
python_docx==0.8.10 | ||
pywin32==227 | ||
xlwings==0.23.0 | ||
pandas==1.2.0 | ||
matplotlib==3.3.3 | ||
docxtpl==0.11.4 | ||
docx==0.2.4 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os, sys # Standard Python Libraries | ||
import xlwings as xw # pip install xlwings | ||
from docxtpl import DocxTemplate # pip install docxtpl | ||
import pandas as pd # pip install pandas | ||
import matplotlib.pyplot as plt # pip install matplotlib | ||
import win32com.client as win32 # pip install pywin32 | ||
|
||
# -- Documentation: | ||
# python-docx-template: https://docxtpl.readthedocs.io/en/latest/ | ||
|
||
# Change path to current working directory | ||
os.chdir(sys.path[0]) | ||
|
||
|
||
def create_barchart(df, barchart_name): | ||
"""Group DataFrame by sub-category, plot barchart, save plot as PNG""" | ||
top_products = df.groupby(by=df["Sub-Category"]).sum()[["Sales"]] | ||
top_products = top_products.sort_values(by="Sales") | ||
plt.rcParams["figure.dpi"] = 300 | ||
plot = top_products.plot(kind="barh") | ||
fig = plot.get_figure() | ||
fig.savefig(f"{barchart_name}.png", bbox_inches="tight") | ||
return None | ||
|
||
|
||
def convert_to_pdf(doc): | ||
"""Convert given word document to pdf""" | ||
word = win32.DispatchEx("Word.Application") | ||
new_name = doc.replace(".docx", r".pdf") | ||
worddoc = word.Documents.Open(doc) | ||
worddoc.SaveAs(new_name, FileFormat=17) | ||
worddoc.Close() | ||
return None | ||
|
||
|
||
def main(): | ||
wb = xw.Book.caller() | ||
sht_panel = wb.sheets["PANEL"] | ||
sht_sales = wb.sheets["Sales"] | ||
doc = DocxTemplate("Sales_Report_TEMPLATE.docx") | ||
# -- Get values from Excel | ||
context = sht_panel.range("A2").options(dict, expand="table", numbers=int).value | ||
df = sht_sales.range("A1").options(pd.DataFrame, index=False, expand="table").value | ||
|
||
# -- Create Barchart & Replace Placeholder | ||
barchart_name = "sales_by_subcategory" | ||
create_barchart(df, barchart_name) | ||
doc.replace_pic("Placeholder_1.png", f"{barchart_name}.png") | ||
|
||
# -- Render & Save Word Document | ||
output_name = f'Sales_Report_{context["month"]}.docx' | ||
doc.render(context) | ||
doc.save(output_name) | ||
|
||
# -- Convert to PDF [OPTIONAL] | ||
path_to_word_document = os.path.join(os.getcwd(), output_name) | ||
convert_to_pdf(path_to_word_document) | ||
|
||
# -- Show Message Box [OPTIONAL] | ||
show_msgbox = wb.macro("Module1.ShowMsgBox") | ||
show_msgbox("DONE!") | ||
|
||
|
||
if __name__ == "__main__": | ||
xw.Book("word_automation.xlsm").set_mock_caller() | ||
main() |
Binary file not shown.