Simplify common Excel operations.
pip install excelmagic
from excel_magic.dataset import open_file
file = open_file('test.xlsx')
Also supports with statement:
from excel_magic.dataset import open_file
with open_file('test.xlsx') as file:
pass
Example data:
Id | Name | Age | Score |
---|---|---|---|
1 | John | 22 | 89 |
2 | David | 23 | 93 |
3 | Emma | 22 | 95 |
Query rows of a sheet in an excel file with specific cell value:
from excel_magic.dataset import open_file
with open_file('test.xlsx') as excel:
# select a sheet by index or sheet name
sheet = excel.get_sheet_by_index(0)
# find rows containing the name 'David'
rows = sheet.find(Name='David')
Or query rows with callback function:
from excel_magic.dataset import open_file
def score_over_90(rows):
if rows['Score'].value > 90:
return True
with open_file('test.xlsx') as excel:
sheet = excel.get_sheet_by_index(0)
# find rows with the score column greater than 90
rows = sheet.filter(score_over_90)
You can use key: value
method to get the cell object in a rod, like operating a dict.
cell = row['Score']
And get the value of the cell object through the value
attribute.
score_num = cell.value
Split multiple sheets of excel file to independent excel files.
from excel_magic.dataset import open_file
file = open_file('test.xlsx')
file.split_sheets_to_files()
Combine sheets from files into a new excel file.
from excel_magic.dataset import open_file
excel_files = ['01.xlsx', '02.xlsx', '03.xlsx']
new_excel = open_file('test.xlsx')
for file in excel_files:
new_excel.merge_file(file)
new_excel.save()
Or
from excel_magic.dataset import open_file
excel_files = ['01.xlsx', '02.xlsx', '03.xlsx']
with open_file('test.xlsx') as new_excel:
for file in excel_files:
new_excel.merge_file(file)
The hierarchical relationship in the excel file is:
Excel (sheets) → Sheet → Row → Cell
And excelmagic provides similar hierarchical object API:
Dataset Object → Sheet Object → Row Object → Cell Object
Example:
from excel_magic.dataset import open_file
dataset = open_file('test.xlsx')
Methods:
Search Sheet
get_sheet_by_index(index: int) -> Sheet
get_sheet_by_name(name: str) -> Sheet
does_exist(name: str) -> bool
Create Sheet
add_sheet(name: str, fields: List[str]) -> Sheet
Delete Sheet
remove_sheet(sheet: Sheet) -> None
Others
save() -> None
split_sheets_to_files() -> None
merge_file(path: str) -> None
export_json(out: str) -> None
Example:
from excel_magic.dataset import open_file
dataset = open_file('test.xlsx')
sheet = dataset.get_sheet_by_index(0)
Methods:
Search rows
find(**kwargs: dict[str, Any]) -> List[dict]
filter(callback: Callable[[dict], Union[None, bool]]) -> List[dict]
get_rows() -> List[dict]
Create row
append_row(content: Union[dict, List[str]]) -> None
Delete row
remove_row(row: dict) -> None
Export and Import sheet
to_csv(out: str = '') -> None
to_json(out: str = '') -> None
import_json(path: str)
Others:
print_row(index: int) -> str
beautify(by: str) -> List[dict]
set_header_style(style: Style) -> None
set_row_style(row: Union[dict, int], style: Style) -> None
Example:
from excel_magic.dataset import open_file
dataset = open_file('test.xlsx')
sheet = dataset.get_sheet_by_index(0)
rows = sheet.find(Name='David') # return a list of found row object
Methods:
The row object is dict-type, with column headers as its key and cell object as the value.
So you can get the cell object of a row with row[key]
or row.get(key)
, like dict type dose.
Read cell
row[key].value
Update cell
row[key].value = new_value
Delete cell
row[key].value = ''
Example:
from excel_magic.dataset import open_file
dataset = open_file('test.xlsx')
sheet = dataset.get_sheet_by_index(0)
rows = sheet.find(Name='David') # return a list of found row object
cell = rows[0].get('Score') # then use value attribute to get the value of a cell
score = cell.value
Attributes:
value
Methods:
set_style(style: Style) -> None
Create the style object for cells.
Example:
from excel_magic.dataset import Style
my_style = Style()
my_style.fill_color = '#52de97'
my_style.font_size = 20
my_style.bold = True
cell.set_style(my_style)
The following attributes have been supported:
Attribute | Optional Value | Default Value |
---|---|---|
font_color | 'red' or '(255, 0, 0)' or '#FF0000' ... | 'black' |
fill_color | 'red' or '(255, 0, 0)' or '#FF0000' ... | '' |
font_name | 'Calibri' or 'Times New Roman' or 'Arial' ... | 'Calibri' |
font_size | 12 or '12' ... | 12 |
bold | True or False | False |
underline | True or False | False |
horizontal_alignment | 'left' or 'center' or 'right' | 'left' |
vertical_alignment | 'top' or 'center' or 'bottom' | 'top' |
Kelly
See also the list of contributors who participated in this project.
Version | Tag | Published |
---|---|---|
2.0.10 | 3yrs ago | |
2.0.8 | 3yrs ago | |
2.0.7 | 3yrs ago | |
2.0.6 | 3yrs ago |