Skip to content

Commit f8d88ce

Browse files
authored
Merge pull request #181 from Wacky404/feature/css_files_to_html_str
feat: added the ability for css_files to be used on html_str when using screenshot function.
2 parents 2ed6b96 + c0773df commit f8d88ce

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ If you want to contribute to `html2image` or run tests locally, follow these ste
359359

360360
4. **Install dependencies (including development tools):**
361361
```bash
362-
uv pip install -e .[dev]
362+
uv pip install -e ".[dev]"
363363
```
364364
This installs the package in editable mode along with all dependencies needed for testing and linting.
365365

html2image/html2image.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,29 @@ def _prepare_html_string(html_body, css_style_string):
421421
"""
422422
return dedent(prepared_html)
423423

424+
@staticmethod
425+
def _prepare_css_str(css_file):
426+
""" Creates a basic string fromatted from a css file.
427+
428+
Parameters
429+
----------
430+
- `css_file`: str
431+
432+
Returns
433+
-------
434+
- str
435+
The contents of each css file.
436+
437+
"""
438+
css_str = ""
439+
for css in css_file:
440+
temp_css_str = ''
441+
with open(css, "r") as fd:
442+
temp_css_str = fd.read()
443+
css_str += temp_css_str + '\n'
444+
445+
return css_str
446+
424447
def screenshot(
425448
self,
426449
html_str=[], # html_str: Union[str, list] = [],
@@ -515,7 +538,8 @@ def screenshot(
515538
base_name, _ = os.path.splitext(name)
516539
html_filename = base_name + '.html'
517540
content = Html2Image._prepare_html_string(
518-
html, css_style_string
541+
html, css_style_string if css_style_string != '' else Html2Image._prepare_css_str(
542+
css_file)
519543
)
520544
self.load_str(content=content, as_filename=html_filename)
521545
self.screenshot_loaded_file(

0 commit comments

Comments
 (0)