vgalin/html2image

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

michaelrussell4 opened this issue · 3 comments

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

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.

Of course, that's even better.

K I committed the change you suggested in my PR.