-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_export.py
198 lines (134 loc) · 5.28 KB
/
word_export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
import tkinter.messagebox
import docx_functions
from docx import Document
from docx.enum.text import WD_BREAK
import openpyxl
import excel_functions
import file_menu
import pandas as pd
class Export_Word_Excel_Window(object):
def __init__(self, mainapp, master, mode, parent_component, file_type='word'):
#self.drawing_dictionary = drawing_dictionary
top=self.top=Toplevel(master)
top.grab_set()
self.mainapp = mainapp
self.mode = mode
self.parent_component = parent_component
self.file_type = file_type
self.setup_label_frames()
self.setup_widgets()
self.button = 'cancel'
if 'json' not in file_type:
self.component_type = parent_component.backend.type
else:
self.component_type = None
def setup_label_frames(self):
self.main_frame = LabelFrame(self.top, text='Options:')
self.main_frame.grid(row=2, column=0, columnspan = 4, rowspan = 1,sticky='NW',padx=5, pady=5, ipadx=2, ipady=5)
def setup_widgets(self):
Label(self.main_frame, text='Output File:').grid(row=1, column = 1, ipadx=1, ipady=1, padx=2, pady=2, sticky='NW')
self.file_entry = Entry(self.main_frame, width = 80)
self.file_entry.grid(row=1, column = 2, ipadx=1, ipady=1, padx=2, pady=2, sticky='NW')
if self.file_type == 'word':
self.file_entry.insert(0, r'C:/Users/domhn/Documents/Python/Pycabin_Tkinter/V0.18/test.docx')
else:
self.file_entry.insert(0, r'C:/Users/domhn/Documents/Python/Pycabin_Tkinter/V0.18/test.xlsx')
Button(self.main_frame, text='Browse', command=self.browse).grid(row=1, column = 3, ipadx=1, ipady=1, padx=2, pady=2, sticky='NW')
# ok button
self.ok_button=Button(self.top,text='OK', command= lambda button = 'ok': self.cleanup(button))
self.ok_button.grid(row=8,column=1, padx=5, pady=5,sticky="ne")
# cancel button
self.b=Button(self.top,text='Cancel', command= lambda button = 'cancel': self.cleanup(button))
self.b.grid(row=8,column=2, padx=5, pady=5,sticky="nw")
def browse(self):
filename = filedialog.asksaveasfilename()
self.file_entry.delete(0, 'end')
self.file_entry.insert(0, filename)
def cleanup(self, button):
self.button = button
if self.button == 'ok':
self.filename = self.file_entry.get()
#Checks
msg = None
if self.file_type == 'word':
# is extension .docx
if self.filename[-5:] != '.docx':
msg = 'Extension must be .docx'
elif self.file_type == 'excel':
if self.filename[-5:] != '.xlsx':
msg = 'Extension must be .xlsx'
elif self.file_type == 'dxf':
if self.filename[-4:] != '.dxf':
msg = 'Extension must be .dxf'
elif self.file_type == 'load json' or self.file_type == 'save json':
if self.filename[-5:] != '.json':
msg = 'Extension must be .json'
#
if msg:
tkinter.messagebox.showerror(master=self.top, title='Error', message=msg)
else:
if self.file_type == 'word':
saved = self.export_word_doc()
elif self.file_type == 'excel':
saved = self.export_excel_file()
elif self.file_type == 'dxf':
self.parent_component.draw_dxf(self.filename)
saved=True
elif self.file_type == 'load json':
saved = self.load_json()
if saved:
self.top.destroy()
else:
self.top.destroy()
def export_word_doc(self):
self.document = Document()
parts = self.parent_component.backend.gen_parts_table()
#SB table
docx_functions.write_table(self.document, ['Qty', 'Part Number', 'Description'],parts)
p = self.document.add_paragraph()
run = p.add_run()
run.add_break(WD_BREAK.PAGE)
ipc_table = self.parent_component.backend.gen_ipc_table()
docx_functions.write_table(self.document, ['ITEM NO.', 'PART NUMBER', 'DESCRIPTION', 'FROMTO', 'QTY'],ipc_table)
try:
self.document.save(self.filename)
saved = True
except PermissionError:
tkinter.messagebox.showerror(master=self.top, title='Error', message='Could not save file, permission denied')
saved = False
return saved
def export_excel_file(self):
excel_data = self.parent_component.backend.gen_excel_data()
if excel_data['type'] == 'list':
wb = openpyxl.Workbook()
sheet_styles = excel_functions.setup_styles()
count = 1
for table in excel_data['data']:
wb.create_sheet(index=count, title=(table))
wb.active = count
sheet = wb.active
excel_functions.add_data_to_sheet(wb, sheet, excel_data['data'][table], 1, 1, sheet_styles['Normal'])
count += 1
try:
wb.save(self.filename)
saved = True
except PermissionError:
tkinter.messagebox.showerror(master=self.top, title='Error', message='Could not save file, permission denied')
saved = False
elif excel_data['type'] == 'df':
with pd.ExcelWriter(self.filename) as writer:
for table in excel_data['data']:
excel_data['data'][table].to_excel(writer, sheet_name=table)
saved=True
return saved
def load_json(self):
try:
file_menu.load_file(self.filename)
saved=True
except:
tkinter.messagebox.showerror(master=self.top, title='Error', message='Failed to Load File')
saved = False