vgalin / html2image

A package acting as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.
MIT License
344 stars 44 forks source link

File names or directories with periods in them don't work #129

Closed michaelrussell4 closed 10 months ago

michaelrussell4 commented 11 months ago

File names with periods in them don't work because the html2image.py currently splits the file name on the first period, assuming there is only one such as in filename.png for files in the save_as list. But for a file name such as file.name.with.periods.png it breaks because it converts it to file.html (`html_filename = name.split('.')[0] + '.html'). This is a huge problem when directories have periods in them because then the paths get ruined.

C:\Users\my.name\Documents\project\file.name.png

becomes

C:\Users\my.html

Simple fix. Just change the code to split only on the last period.

html_filename = ''.join(name.split('.')[:-1]) + '.html'
vgalin commented 11 months ago

Hello, thank you for addressing this issue. I noticed the fix uses string manipulation to split the file name. Could I suggest a slightly more robust approach using os.path.splitext? It's specifically designed for splitting file names and extensions and might be better suited for this purpose. In retrospective, this is how I should have done it from the start ...

Here's a possible implementation:

base_name, _ = os.path.splitext(name)
html_filename = base_name + '.html'

Thanks for considering this suggestion, this could be done via a new commit or a different PR.

michaelrussell4 commented 11 months ago

Of course, that's even better.

michaelrussell4 commented 11 months ago

K I committed the change you suggested in my PR.