-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileHandling.py
More file actions
269 lines (211 loc) · 12.2 KB
/
Copy pathFileHandling.py
File metadata and controls
269 lines (211 loc) · 12.2 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from PyQt5.QtWidgets import (QTableWidget, QLineEdit)
import datetime
from docx import Document
from docx.shared import Inches
from bs4 import BeautifulSoup
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
import os
import shutil
import re
import sys
class FileManipulation():
def __init__(self, table_widget: QTableWidget, two_week_table: QTableWidget, name_input: QLineEdit):
self.table_widget = table_widget
self.two_week_table = two_week_table
self.name_input = name_input
def notOnlyLetters(self, name):
# Return True if anything other than characters and spaces
return bool(re.search(r'[^a-zA-Z ]', name)) #The ^ inside the brackets negates the character class, meaning the pattern matches any character except letters and spaces.
def generate_docx(self, filename):
if hasattr(sys, '_MEIPASS'):
base_directory = sys._MEIPASS # Path to the folder where bundled files are stored
else:
base_directory = os.path.dirname(os.path.abspath(__file__))
information1_path = os.path.join(base_directory, 'information1.txt')
information2_path = os.path.join(base_directory, 'information2.txt')
today = datetime.date.today()
doc = Document()
# Add title
doc.add_heading('Rest Period Acknowledgement Form', 0)
# Add formatted content from information1.txt
info1_content = self.read_and_format_file(information1_path)
self.add_html_content(doc, info1_content)
# Add Table Data
doc.add_paragraph('Table Data:')
self.add_table(doc, self.table_widget)
doc.add_paragraph("")
# Add formatted content from information2.txt
info2_content = self.read_and_format_file(information2_path)
self.add_html_content(doc, info2_content)
# Add Two Week Table Data
doc.add_paragraph('Two Week Table:')
self.add_table(doc, self.two_week_table)
# Add Name with formatting
doc.add_paragraph("")
doc.add_paragraph(f"Name: {self.name_input.text()}")
# Add Signature and date
doc.add_paragraph(f"Signature: ______________________________________________________ Date: {today}")
# Save the document
doc.save(filename)
def add_html_content(self, doc, html_content):
# Use BeautifulSoup to parse HTML content
soup = BeautifulSoup(html_content, 'html.parser')
# Convert HTML content to Word paragraphs
for element in soup:
if element.name == 'p': # Paragraphs
doc.add_paragraph(element.get_text())
elif element.name == 'br': # Line breaks
doc.add_paragraph() # Add an empty paragraph for line break
def add_table(self, doc, table_widget):
num_rows = table_widget.rowCount()
num_cols = table_widget.columnCount()
# Check if the table has headers (e.g., days of the week)
has_headers = num_rows == 3
# Create the table
if has_headers:
# Create table with an extra row for headers
table = doc.add_table(rows=num_rows + 1, cols=num_cols)
header_row = table.rows[0] # Header row
# Add headers
headers = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
for col_idx, header in enumerate(headers):
cell = header_row.cells[col_idx]
cell.text = header
# Set font size for header
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(10)
cell.paragraphs[0].paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
# Fill the rest of the table with data
for row in range(1, num_rows + 1): # Skip the header row
for col in range(num_cols):
cell = table.cell(row, col)
item = table_widget.item(row - 1, col) # Adjust index for header
cell.text = item.text() if item else ''
# Set font size and alignment
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(8)
paragraph.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
else:
# Create table without headers
table = doc.add_table(rows=num_rows, cols=num_cols)
# Fill the table with data
for row in range(num_rows):
for col in range(num_cols):
cell = table.cell(row, col)
item = table_widget.item(row, col)
cell.text = item.text() if item else ''
# Set font size and alignment
for paragraph in cell.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(11)
paragraph.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
# Set column widths if necessary
for col in table.columns:
for cell in col.cells:
cell.width = Inches(1.0)
# Set uniform row height
uniform_row_height = Pt(14)
for row in table.rows:
for cell in row.cells:
# Set the height of each cell
cell.height = uniform_row_height
for paragraph in cell.paragraphs:
paragraph.paragraph_format.space_after = Pt(0)
paragraph.paragraph_format.space_before = Pt(0)
# Optional: Set table formatting
table.style = 'Table Grid'
# Add an empty paragraph to ensure no extra spacing before the table
doc.add_paragraph()
def delete_doc(self, filename):
# deletes doc file if it exists
if os.path.isfile(filename):
os.remove(filename)
def move_pdf(self, filename):
# Get the source path (same directory as main.py)
source_path = filename
# Determine the base directory depending on operating system
if os.name == 'nt': # Windows
base_directory = os.environ.get('USERPROFILE', '') # This gets 'C:\Users\Username'
elif os.name == 'posix': # macOS/Linux
base_directory = os.environ.get('HOME', '') # This gets '/home/username'
else:
raise EnvironmentError('Unsupported operating system')
start_date = self.two_week_table.item(0, 0).text() # grab the start date from the table
end_date = self.two_week_table.item(1, 6).text() # grab the end date from the table
# Define the rest of the path relative to the base directory
dates = f"PayPeriod_{start_date}_{end_date}"
relative_path = os.path.join('Special Services Group, LLC', 'SSG Customer Access - Documents', 'Rest Period Acknowledgement Form' ,dates)
# Construct the full destination path
destination_directory1 = os.path.join(base_directory, relative_path)
destination_path1 = os.path.join(destination_directory1, filename)
destination_directory2 = os.path.join("D:", r'\Special Services Group, LLC', 'SSG Customer Access - Documents', 'Rest Period Acknowledgement Form', dates)
destination_path2 = os.path.join(destination_directory2, filename)
destination1_exists = os.path.join(base_directory, 'Special Services Group, LLC', 'SSG Customer Access - Documents')
destination2_exists = os.path.join('D:', r'\Special Services Group, LLC', 'SSG Customer Access - Documents')
# Check if the source file exists
if os.path.isfile(source_path):
# Handle the first destination
if os.path.exists(destination1_exists):
if not os.path.exists(destination_directory1):
os.makedirs(destination_directory1) # Create the directory if it does not exist
shutil.move(source_path, destination_path1)
# Handle the second destination
if os.path.exists(destination2_exists):
if not os.path.exists(destination_directory2):
os.makedirs(destination_directory2) # Create the directory if it does not exist
shutil.move(source_path, destination_path2)
def read_and_format_file(self, filename):
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
# Directly return content if it's in proper HTML format
return content
def transfer_all_file_button(self):
if os.name == 'nt': # Windows
base_directory = os.environ.get('USERPROFILE', '') # This gets 'C:\Users\Username'
elif os.name == 'posix': # macOS/Linux
base_directory = os.environ.get('HOME', '') # This gets '/home/username'
else:
raise EnvironmentError('Unsupported operating system')
c_path = os.path.join('Special Services Group, LLC', 'SSG Customer Access - Documents', 'Rest Period Acknowledgement Form')
d_path = os.path.join("D:", r'\Special Services Group, LLC', 'SSG Customer Access - Documents', 'Rest Period Acknowledgement Form')
source1 = os.path.join(base_directory, c_path)
source2 = d_path
destination1 = os.path.join(base_directory, r"Special Services Group, LLC\SSG Office Admin - Documents\Training - Internal\Rest Period Acknowledgement Form")
destination2 = os.path.join('D:', r'\Special Services Group, LLC', 'SSG Office Admin - Documents', 'Training - Internal', 'Rest Period Acknowledgement Form')
if os.path.exists(source1):
payperiod_folder = os.listdir(source1)
for folder in payperiod_folder:
folder_path = os.path.join(source1, folder)
folder_destination = os.path.join(destination1, folder)
if os.path.exists(folder_destination):
# If it exists, copy the contents instead of the folder
for item in os.listdir(folder_path):
source_item = os.path.join(folder_path, item)
destination_item = os.path.join(folder_destination, item)
if os.path.isdir(source_item):
# If the item is a directory, copy it and its contents
shutil.copytree(source_item, destination_item, dirs_exist_ok=True)
else:
# If the item is a file, copy it
shutil.copy2(source_item, destination_item)
shutil.rmtree(folder_path)
# This copies the whole payperiod folder with the files in it and copies it to the destination
else:
shutil.copytree(folder_path, folder_destination)
# Delete the old folder
shutil.rmtree(folder_path)
elif os.path.exists(source2):
payperiod_folder = os.listdir(source2)
for folder in payperiod_folder:
folder_path = os.path.join(source2, folder)
folder_destination = os.path.join(destination2, folder)
# This copies the whole payperiod folder with the files in it and copies it to the destination
shutil.copytree(folder_path, folder_destination)
# Delete the old folder
shutil.rmtree(folder_path)
else:
return False
return True