-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (53 loc) · 2.18 KB
/
main.py
File metadata and controls
70 lines (53 loc) · 2.18 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
import csv
import os
import docx
CSV_PATH = "data"
DOCX_PATH = "result"
DELIMITER = "|"
def allowed_file(filename: str, allowed_extensions: tuple[str, ...]) -> bool:
"""
Check a file extension
:param filename: a file name
:param allowed_extensions: a list of valid file extensions
:return: an allowed file extension flag
"""
return "." in filename and filename.rsplit(".", 1)[1] in allowed_extensions
if __name__ == "__main__":
if not os.path.exists(CSV_PATH):
os.makedirs(CSV_PATH)
if not os.path.exists(DOCX_PATH):
os.makedirs(DOCX_PATH)
total_col_count, total_cell_count, total_empty_cell_count = 0, 0, 0
for _, _, files in os.walk(CSV_PATH):
for file in files:
if not allowed_file(file, ("csv",)):
continue
with open(CSV_PATH + "/" + file, newline="", encoding="utf-8") as f:
csv_reader = csv.reader(f, delimiter=DELIMITER)
csv_headers = next(csv_reader)
csv_cols = len(csv_headers)
total_col_count += csv_cols
doc = docx.Document()
table = doc.add_table(rows=1, cols=csv_cols)
hdr_cells = table.rows[0].cells
for i in range(csv_cols):
hdr_cells[i].text = csv_headers[i]
if not hdr_cells[i].text:
total_empty_cell_count += 1
row_count = 1
for row in csv_reader:
row_count += 1
row_cells = table.add_row().cells
for i in range(csv_cols):
row_cells[i].text = row[i]
if not row_cells[i].text:
total_empty_cell_count += 1
total_cell_count += (row_count * csv_cols)
doc.add_page_break()
result_path = DOCX_PATH + "/" + os.path.splitext(file)[0] + ".docx"
doc.save(result_path)
print("Save DOCX files:")
print(result_path)
print(f"Total cols = {total_col_count}")
print(f"Total cells = {total_cell_count}")
print(f"Total empty cells = {total_empty_cell_count}")