|
| 1 | +# API Reference |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## tinybear.csv_xls |
| 6 | + |
| 7 | +### append_empty_column_to_csv |
| 8 | +```python |
| 9 | +def append_empty_column_to_csv(path_to_file: Path, name_of_new_column: str, delimiter: CSVDelimiter = ",", custom_path_to_output_file: Optional[Path] = None) -> None: |
| 10 | +``` |
| 11 | +Adds empty column (as last column) to CSV file. **Overwrites file**, but optional output path can be specified to create a new file. |
| 12 | + |
| 13 | +Raises: |
| 14 | +- ValueError if column name already exists in file. |
| 15 | +- FileExistsError if custom output file is specified and already exists. |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +### check_csv_for_malformed_rows |
| 20 | +```python |
| 21 | +def check_csv_for_malformed_rows(path_to_file: Path) -> None: |
| 22 | +``` |
| 23 | +Checks whether all rows in CSV file have the same number of columns. Throws IndexError if they do not. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +### check_csv_for_repetitions_in_column |
| 28 | +```python |
| 29 | +def check_csv_for_repetitions_in_column(path_to_file: Path, column_name: str) -> None: |
| 30 | +``` |
| 31 | +Throws ValueError if there are repetitions in given column of given file. |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +### convert_xls_to_csv |
| 36 | +```python |
| 37 | +def convert_xls_to_csv(path_to_input_excel_file: Path, sheet_name: str, path_to_output_csv_file: Path, delimiter: CSVDelimiter = ",", overwrite: bool = True) -> None: |
| 38 | +``` |
| 39 | +Converts sheet from Excel file to CSV format. |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +### read_column_from_csv |
| 44 | +```python |
| 45 | +def read_column_from_csv(path_to_file: Path, column_name: str) -> list[str]: |
| 46 | +``` |
| 47 | +Reads one column from CSV file. Column name is taken from the top row. Raises KeyError if no such column exists. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +### read_dicts_from_csv |
| 52 | +```python |
| 53 | +def read_dicts_from_csv(path_to_file: Path, delimiter: CSVDelimiter = ",") -> list[dict[str, str]]: |
| 54 | +``` |
| 55 | +Reads CSV as list of dictionaries (top row is considered key). |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +### read_dict_from_2_csv_columns |
| 60 | +```python |
| 61 | +def read_dict_from_2_csv_columns(path_to_file: Path, key_col: str, val_col: str, delimiter: CSVDelimiter = ",") -> dict[str, str]: |
| 62 | +``` |
| 63 | +Reads CSV and returns dict mapping keys from key_col to values from val_col. |
| 64 | + |
| 65 | +--- |
| 66 | + |
| 67 | +### read_dicts_from_xls |
| 68 | +```python |
| 69 | +def read_dicts_from_xls(path_to_file: Path, sheet_name: str) -> list[dict[str, str]]: |
| 70 | +``` |
| 71 | +Reads XLS sheet as list of dictionaries (top row as key). |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +### read_plain_rows_from_csv |
| 76 | +```python |
| 77 | +def read_plain_rows_from_csv(path_to_file: Path, delimiter: CSVDelimiter = ",", remove_1st_row: bool = False) -> list[list[str]]: |
| 78 | +``` |
| 79 | +Reads plain rows (list of lists) from CSV. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +### remove_rows_with_given_content_in_lookup_column |
| 84 | +```python |
| 85 | +def remove_rows_with_given_content_in_lookup_column(rows: list[dict[str, str]], lookup_column: str, match_value: str) -> tuple[list[dict[str, str]], tuple[int, ...]]: |
| 86 | +``` |
| 87 | +Remove rows where lookup_column matches match_value. Returns (new list, indices of removed rows). |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +### write_csv |
| 92 | +```python |
| 93 | +def write_csv(rows, path_to_file: Path, overwrite: bool, delimiter: CSVDelimiter) -> None: |
| 94 | +``` |
| 95 | +Writes rows (various formats) to CSV file. Adds header if writing dicts/NamedTuples. |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +## tinybear.json_toml_yaml |
| 100 | + |
| 101 | +### check_yaml_file |
| 102 | +```python |
| 103 | +def check_yaml_file(path_to_file: Path, verbose: bool = True) -> None: |
| 104 | +``` |
| 105 | +Validates YAML file, throws if malformed or duplicate top-level keys are found. |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### read_json_toml_yaml |
| 110 | +```python |
| 111 | +def read_json_toml_yaml(path_to_file: Path) -> Union[dict[str, Any], list[str]]: |
| 112 | +``` |
| 113 | +Auto-detects file extension and deserializes JSON, TOML, or YAML to Python types. |
| 114 | + |
| 115 | +--- |
| 116 | + |
| 117 | +## tinybear.txt |
| 118 | + |
| 119 | +### check_encoding_of_file |
| 120 | +```python |
| 121 | +def check_encoding_of_file(file: Path) -> str: |
| 122 | +``` |
| 123 | +Check encoding (utf-8 or cp1251/ANSI); returns detected encoding. |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +### read_non_empty_lines_from_txt_file |
| 128 | +```python |
| 129 | +def read_non_empty_lines_from_txt_file(path_to_file: Path) -> list[str]: |
| 130 | +``` |
| 131 | +Gets non-empty lines from TXT file as list. |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +### read_plain_text_from_file |
| 136 | +```python |
| 137 | +def read_plain_text_from_file(path_to_file: Path) -> str: |
| 138 | +``` |
| 139 | +Reads plain text from file (utf-8 or cp1251 encoding). |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +### remove_extra_space |
| 144 | +```python |
| 145 | +def remove_extra_space(str_: str) -> str: |
| 146 | +``` |
| 147 | +Removes leading/trailing/multiple spaces in a string. |
| 148 | + |
| 149 | +--- |
| 150 | + |
| 151 | +### write_plain_text_to_file |
| 152 | +```python |
| 153 | +def write_plain_text_to_file(content: Union[str, list[str], tuple[str]], file: Path, overwrite: bool, newline_char: str = "\n") -> None: |
| 154 | +``` |
| 155 | +Writes string or lines to text file. Optionally enforces overwrite/newlines. |
| 156 | + |
| 157 | +--- |
| 158 | + |
| 159 | +### move_line |
| 160 | +```python |
| 161 | +def move_line(file: Path, line_number_to_cut: int, line_number_to_insert_before: Union[int, Literal["END"]], output_file: Union[Path, None] = None) -> None: |
| 162 | +``` |
| 163 | +Moves a line in a text file to another position; saves to output file if given. |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## tinybear.html.validate_html |
| 168 | + |
| 169 | +### validate_html |
| 170 | +```python |
| 171 | +def validate_html(html: str, allowed_tags: Iterable[str] = (...), is_text_at_root_level_allowed: bool = False) -> None: |
| 172 | +``` |
| 173 | +Validate HTML string for allowed tags, structure, and correct entities. Raises ParsingError on errors. |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## tinybear.html.from_docx |
| 178 | + |
| 179 | +### convert_file_from_doc |
| 180 | +```python |
| 181 | +def convert_file_from_doc(path_to_file: Path, output_dir: Path = DEFAULT_OUTPUT_DIR, style_map: str = DEFAULT_STYLE_MAP, print_html: bool = True) -> Path: |
| 182 | +``` |
| 183 | +Read from DOC(x), write to HTML file, return output path. |
| 184 | + |
| 185 | +--- |
| 186 | + |
| 187 | +### convert_all_docs |
| 188 | +```python |
| 189 | +def convert_all_docs(input_dir: Path = DEFAULT_INPUT_DIR, output_dir: Path = DEFAULT_OUTPUT_DIR, print_html: bool = True) -> None: |
| 190 | +``` |
| 191 | +Convert all .DOC(x) files in a directory to HTML. |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +### read_from_doc |
| 196 | +```python |
| 197 | +def read_from_doc(path_to_file: Path, style_map: str = DEFAULT_STYLE_MAP) -> str: |
| 198 | +``` |
| 199 | +Read binary DOCX file, return HTML string. |
| 200 | + |
| 201 | +--- |
| 202 | + |
| 203 | +## tinybear.exceptions |
| 204 | + |
| 205 | +### ParsingError |
| 206 | +```python |
| 207 | +class ParsingError(Exception): |
| 208 | + """Base class for all parsing errors.""" |
| 209 | +``` |
0 commit comments