Excel Manager
This page contains documentation for the
Date Manager
package which is curated with frequently used datetime methods,
modules in a simpler way.
Frequent excel operations using methods provided.
Modules exported by this package:
excel_manager
: Provide several frequently used excel operations.
Package Details
This module allows the user to make excel operations.
Examples:
>>> from excel_manager.excel_manager import ExcelManager
>>> excel_manager = ExcelManager(log_file='abc.log')
>>> workbook = "test.xlsx"
>>> sheet = "Sheet1"
>>> dataframe = excel_manager.get_dataframe(workbook, sheet)
>>> import pandas as pd
>>> workbook = "test.xlsx"
>>> sheet = "Sheet1"
>>> dataframe = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> excel_manager.overwrite_sheet(workbook, sheet, dataframe)
>>> import pandas as pd
>>> workbook = "test.xlsx"
>>> sheet = "Sheet1"
>>> dataframe = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
>>> excel_manager.append_dataframe(workbook, sheet, dataframe)
>>> # to set the protection, 'abc' is password.
>>> workbook = "test.xlsx"
>>> sheet = "Sheet1"
>>> excel_manager.modify_sheet_protection(workbook, sheet, True, 'abc')
>>> # to remove the protection.
>>> workbook = "test.xlsx"
>>> sheet = "Sheet1"
>>> excel_manager.modify_sheet_protection(workbook, sheet, False)
The module contains the following functions:
__init__(log_file)
- creates the instance of the class.get_dataframe(workbook, sheet)
- returns the dataframe from excel workbook.delete_sheet(workbook, sheet)
- deletes the sheet from excel workbook.create_sheet(workbook, sheet)
- creates a new sheet in excel workbook.overwrite_sheet(workbook, sheet, dataframe)
- overwrites the content of the sheet.reposition_sheet(workbook, sheet)
- reposition the sheet in excel workbook.append_dataframe(workbook, sheet, dataframe)
- append the dataframe to the content in excel sheet.modify_sheet_protection(workbook, sheet, True, 'abc')
- adds or removes the protection from sheet in excel workbook.
ExcelManager
Source code in excel_manager\excel_manager.py
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 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
|
__init__(log_file='./Custom-Python_Tools.log')
Parameters:
Name | Type | Description | Default |
---|---|---|---|
log_file |
str
|
The path to the log file. |
'./Custom-Python_Tools.log'
|
Source code in excel_manager\excel_manager.py
append_dataframe(workbook, sheet, dataframe, password=None)
Appends a pandas dataframe to an Excel sheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workbook |
str
|
The path to the Excel workbook. |
required |
sheet |
str
|
The name of the sheet to which the dataframe will be appended. |
required |
dataframe |
DataFrame
|
The pandas dataframe to be appended to the sheet. |
required |
password |
str
|
The password for the Excel workbook, if it is protected. |
None
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
PermissionError
|
If the user does not have permission to write to the specified workbook. |
Exception
|
If an unexpected error occurs. |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Returns nothing. |
Source code in excel_manager\excel_manager.py
create_sheet(workbook, sheet)
Create a new sheet in an Excel workbook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workbook |
str
|
The path to the Excel workbook. |
required |
sheet |
str
|
The name of the sheet to be created. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
PermissionError
|
If the user does not have permission to write to the specified workbook. |
Exception
|
If an unexpected error occurs. |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Returns nothing. |
Source code in excel_manager\excel_manager.py
delete_sheet(workbook, sheet)
Delete a sheet from an Excel workbook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workbook |
str
|
The path to the Excel workbook. |
required |
sheet |
str
|
The name of the sheet to be deleted. |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Returns nothing. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
ValueError
|
If the specified sheet does not exist in the workbook. |
Exception
|
If an unexpected error occurs. |
Source code in excel_manager\excel_manager.py
get_dataframe(workbook, sheet)
Retrieve a pandas dataframe from an Excel workbook.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workbook |
str
|
The path to the Excel workbook. |
required |
sheet |
str
|
The name of the sheet containing the data. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The contents of the specified sheet as a pandas dataframe. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
Exception
|
If an unexpected error occurs. |
Source code in excel_manager\excel_manager.py
modify_sheet_protection(filepath, sheetname, enable_protection, password=None)
Modifies the protection of an Excel sheet.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filepath |
str
|
The path to the Excel workbook. |
required |
sheetname |
str
|
The name of the sheet to be protected. |
required |
enable_protection |
bool
|
A boolean value indicating whether to enable or disable protection. |
required |
password |
str
|
The password for the Excel workbook, if it is protected. |
None
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
PermissionError
|
If the user does not have permission to write to the specified workbook. |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Returns nothing. |
Source code in excel_manager\excel_manager.py
overwrite_sheet(workbook, sheet, dataframe)
Overwrite the contents of an Excel sheet with a new dataframe.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workbook |
str
|
The path to the Excel workbook. |
required |
sheet |
str
|
The name of the sheet to be overwritten. |
required |
dataframe |
DataFrame
|
The new contents of the sheet as a pandas dataframe. |
required |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Returns nothing. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
PermissionError
|
If the user does not have permission to write to the specified workbook. |
ValueError
|
If the specified sheet does not exist in the workbook. |
Exception
|
If an unexpected error occurs. |
Source code in excel_manager\excel_manager.py
reposition_sheet(workbook, sheet)
Moves a sheet at the start in the workbook and saves the changes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
workbook |
str
|
The path to the Excel workbook. |
required |
sheet |
str
|
The name of the sheet to be moved. |
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If the specified workbook cannot be found. |
PermissionError
|
If the user does not have permission to write to the specified workbook. |
ValueError
|
If the specified sheet does not exist in the workbook. |
Exception
|
If an unexpected error occurs. |
Returns:
Name | Type | Description |
---|---|---|
None |
None
|
Returns nothing. |