-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_xlsx.py
84 lines (65 loc) · 3.23 KB
/
create_xlsx.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
from openpyxl import styles
import pandas as pd
def create_xlsx_report(data, title, time_period):
# Ensure title is a string
if isinstance(title, list) and len(title) == 1:
title = title[0]
elif not isinstance(title, str):
raise ValueError("Title should be a string")
# Convert data to DataFrame if it's a list of DataFrames
if isinstance(data, list) and all(isinstance(i, pd.DataFrame) for i in data):
data = pd.concat(data)
xlsx_file = f"{title} {time_period}.xlsx"
with pd.ExcelWriter(xlsx_file, engine='openpyxl') as writer: # pylint: disable=abstract-class-instantiated
data.to_excel(writer, index=False, startrow=2) # Start writing CSV data from the third row
# Get the active worksheet
sheet = writer.sheets['Sheet1']
# Set column widths
for column in sheet.columns:
max_length = max(len(str(cell.value)) for cell in column)
sheet.column_dimensions[column[0].column_letter].width = max_length
# Define a cell style for bold and larger font
bold_large_font = styles.Font(bold=True, size=14)
# Insert the title, time period, and total count in the specified cells
title_cell = sheet['B1']
title_cell.value = title
title_cell.font = bold_large_font
date_cell = sheet['D1']
date_cell.value = time_period
date_cell.font = bold_large_font
total_cell = sheet['F1']
total_cell.value = f"Total Count: {len(data)}"
total_cell.font = bold_large_font
return [xlsx_file]
def create_xlsx_report_multi(dataframes, titles, time_period):
files = []
for data, title in zip(dataframes, titles):
# Ensure data is a DataFrame
if isinstance(data, list):
raise ValueError("Each item in dataframes should be a DataFrame")
xlsx_file = f"{title} {time_period}.xlsx"
with pd.ExcelWriter(xlsx_file, engine='openpyxl') as writer: # pylint: disable=abstract-class-instantiated
data.to_excel(writer, index=False, startrow=2)
sheet = writer.sheets['Sheet1']
xlsx_file = f"{title} {time_period}.xlsx"
with pd.ExcelWriter(xlsx_file, engine='openpyxl') as writer: # pylint: disable=abstract-class-instantiated
data.to_excel(writer, index=False, startrow=2)
sheet = writer.sheets['Sheet1']
# Set column widths
for column in sheet.columns:
max_length = max(len(str(cell.value)) for cell in column)
sheet.column_dimensions[column[0].column_letter].width = max_length
# Define a cell style for bold and larger font
bold_large_font = styles.Font(bold=True, size=14)
# Insert the title, time period, and total count in the specified cells
title_cell = sheet['B1']
title_cell.value = title
title_cell.font = bold_large_font
date_cell = sheet['D1']
date_cell.value = time_period
date_cell.font = bold_large_font
total_cell = sheet['F1']
total_cell.value = f"Total Count: {len(data)}"
total_cell.font = bold_large_font
files.append(xlsx_file)
return files