View Single Post
Old 08-09-2017, 09:25 AM   #6
Doitsu
Grand Sorcerer
Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.Doitsu ought to be getting tired of karma fortunes by now.
 
Doitsu's Avatar
 
Posts: 5,741
Karma: 24031403
Join Date: Dec 2010
Device: Kindle PW2
Quote:
Originally Posted by WS64 View Post
How can I rename a HTML file within an epub with a Sigil-plugin?
You can't rename html files with the Sigil plugin API. You'll have to delete the file and add it as a new file with the desired name. (However, this method can only be used if the file doesn't contain any link target ids referenced by other .xhtml files, unless your script will also take care of this.)

For example:

Spoiler:
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def run(bk):
    manifest_id = 'Section0001.xhtml'
    new_file_name = 'new.xhtml'
    
    # get original spine order
    original_spine = bk.getspine()

    # get file contents
    data = bk.readfile(manifest_id)

    # delete file
    bk.deletefile(manifest_id)

    # add new file with the same file contents
    bk.addfile(manifest_id, new_file_name, data, "application/xhtml+xml")

    # restore original spine order
    bk.setspine(original_spine)

    return 0

def main():
    print('I reached main when I should not have\n')
    return -1

if __name__ == "__main__":
    sys.exit(main())


The above code will rename Section0001.xhtml to new.xhtml.

If you prefer to use pure Python code, you can use bk.copy_book_contents_to() to copy the current epub to a temporary folder and then use os.rename() etc. As with the first method, you'll have to update all link targets with your script.
Doitsu is offline   Reply With Quote