Skip to content

Commit

Permalink
Change color for test case and Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
dhkim4-seegene committed Jul 8, 2024
1 parent 8a5ef28 commit 46241a9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 14 deletions.
102 changes: 97 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,113 @@

### Installation

#### To include in your library
```bash
poetry shell
pip3 install git+https://github.com/ampersandor/excel-writer.git
```
#### To develop and customize
```commandline
git clone https://github.com/ampersandor/excel-writer.git
poetry install

poetry shell
```

#### How to use
The client script shows an example about how to use the library.
### How to use

#### Use Example
The client script shows an example about how to use the library.
```bash
cd src
git clone https://github.com/ampersandor/excel-writer.git
cd excel-writer/tests
python3 client.py
```

#### Guide to use excel-writer
Suppose you have a data as below, and you need to make a table with column name, subject, score, and average score.
```python
students = {
"DongHun Kim": [("Math", 99), ("Biology", 60), ("Computer Science", 100)],
"Jiyeon Yoo": [("Math", 70), ("Biology", 90)],
"William Kim": [("Music", 59), ("Art", 73)],
"Judy Yoo": [("Math", 54), ("Computer Science", 55)],
}
```
<img src = "https://github.com/ampersandor/excel-writer/assets/57800485/6317de1b-71b4-49db-a2d9-1e8b9ae72dc7" width="50%" height="50%">

And your goal is to draw a table as above.

##### 1. Make Sheet
```python
# ######################################## Make sheet ########################################
sheet = Sheet(
name="Students", set_zoom=180, freeze_panes=[(2, 0)],
set_rows=[(1, 20.25)], # set header column height as 20.25
set_columns=[(0, 0, 1)], # set 0 to 0 column width as 1
)
```

##### 2. Make Format && Table
```python
# ######################################## Make table ########################################
default_format = Format({"align": "center", "valign": "vcenter", "font_size": 10,
"bold": False, "left": 7, "right": 7})
header_format = Format({"bg_color": "#FDE9D9", "top": 2, "bottom": 2, "bold": True})
table = sheet.get_and_add_table(table_name="Records", draw_from=(1, 1), table_format=default_format,
filter_option=True)
```

##### 3. Make Column
```python
# ######################################## Make columns ########################################
name_col = table.get_and_add_column("Name", width=13.5, format={"left": 2})
name_col.get_and_add_cell("Name", format=header_format.font_color("white").bg_color("#E87A5D"))

subject_col = table.get_and_add_column("Subject", width=20)
subject_col.get_and_add_cell("Subject", format=header_format.font_color("#F3B941").bg_color("#3B5BA5"))

score_col = table.get_and_add_column("Score", width=4.5)
score_col.get_and_add_cell("Score", format=header_format.font_color("#3B5BA5").bg_color("#E87A5D"))

average_col = table.get_and_add_column("Average", width=8, format={"right": 2})
average_col.get_and_add_cell("Average", format=header_format.font_color("#E87A5D").bg_color("#F3B941"))
```

#### 4. Make Cells
```python
# ######################################## Make cells ########################################

for student_name, records in students.items():
total = 0
to_be_merged = []
for subject, score in records:
name_col.get_and_add_cell(student_name)
subject_col.get_and_add_cell(subject)
score_col.get_and_add_cell(score)
total += score

for _ in range(len(records)):
cell = average_col.get_and_add_cell(round(total / len(records), 2))
to_be_merged.append(cell)

sheet.merge(to_be_merged)
table.draw_division(lvl=Divisor.NORMAL)
table.draw_division(lvl=Divisor.THICK)
```
#### 5. Use show method for debug
```python
table.show()
```

#### 6. Generate Excel
```python
sheets = [sheet]
excel_exporter = ExcelExporter("output.xlsx") # excel file name
excel_exporter.write_sheets(sheets)
```




## Contact
DongHun Kim - <ddong3525@naver.com>

Expand Down
18 changes: 9 additions & 9 deletions tests/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def export_student_sheet(students: Dict[str, List[Tuple]]) -> Sheet:

# ######################################## Make columns ########################################
name_col = table.get_and_add_column("Name", width=13.5, format={"left": 2})
name_col.get_and_add_cell("Name", format=header_format)
name_col.get_and_add_cell("Name", format=header_format.font_color("white").bg_color("#E87A5D"))

subject_col = table.get_and_add_column("Subject", width=20)
subject_col.get_and_add_cell("Subject", format=header_format.font_color("white").bg_color("blue"))
subject_col.get_and_add_cell("Subject", format=header_format.font_color("#F3B941").bg_color("#3B5BA5"))

score_col = table.get_and_add_column("Score", width=4.5)
score_col.get_and_add_cell("Score", format=header_format)
score_col.get_and_add_cell("Score", format=header_format.font_color("#3B5BA5").bg_color("#E87A5D"))

average_col = table.get_and_add_column("Average", width=8, format={"right": 2})
average_col.get_and_add_cell("Average", format=header_format)
average_col.get_and_add_cell("Average", format=header_format.font_color("#E87A5D").bg_color("#F3B941"))

# ######################################## Make cells ########################################

Expand Down Expand Up @@ -88,7 +88,7 @@ def export_student_sheet(students: Dict[str, List[Tuple]]) -> Sheet:
def export_sequence_sheet(sequences: List[Tuple]):
default_format = Format({"align": "center", "valign": "vcenter", "font_size": 10,
"bold": False, "left": 7, "right": 7})
header_format = Format({"bg_color": "#FDE9D9", "top": 2, "bottom": 2, "bold": True})
header_format = Format({"top": 2, "bottom": 2, "bold": True})

# ######################################## Make sheet ########################################
sheet = Sheet(
Expand All @@ -104,16 +104,16 @@ def export_sequence_sheet(sequences: List[Tuple]):

# ######################################## Make columns ########################################
project_col = table.get_and_add_column("Project", width=10.5, format={"left": 2})
project_col.get_and_add_cell("Project", format=header_format)
project_col.get_and_add_cell("Project", format=header_format.bg_color("#E87A5D"))

set_col = table.get_and_add_column("Set", width=7)
set_col.get_and_add_cell("Set", format=header_format)
set_col.get_and_add_cell("Set", format=header_format.bg_color("3B5BA5"))

type_col = table.get_and_add_column("Type", width=10)
type_col.get_and_add_cell("Type", format=header_format)
type_col.get_and_add_cell("Type", format=header_format.bg_color("E87A5D"))

sequence_col = table.get_and_add_column("Sequences", width=60, format={"align": "left", "right": 2})
sequence_col.get_and_add_cell("Sequences", format=header_format.align(Align.CENTER))
sequence_col.get_and_add_cell("Sequences", format=header_format.align(Align.CENTER).bg_color("F3B941"))

# ######################################## Make cells ########################################
project_dict = defaultdict(list)
Expand Down

0 comments on commit 46241a9

Please sign in to comment.