Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Creating and Editing

Notices

Reply
 
Thread Tools Search this Thread
Old 06-19-2025, 12:14 PM   #1
Bill Overal
Junior Member
Bill Overal began at the beginning.
 
Bill Overal's Avatar
 
Posts: 2
Karma: 10
Join Date: Jun 2025
Location: Belém, Pará, Brazil
Device: Samsung pad
calibre to EndNote

For individual convertions of a book's .opf file to an .enw file, the following AI-generated Python program functions well. Place the Python script and the .opf files in the same folder, and the .emw files will be generated in the same folder, ready for entry into EndNote. This saves having to generate a BIBText file to be read by JebRef and out-putting as an EndNote text input file. The .opf file, with a little help, could probably be converted into an .RIS file. Unfortunely, there is no way of saving .opf files to disk, so you have to dig for them. The Python script is the following:

import xml.etree.ElementTree as ET
import os

def opf_to_enw(opf_path):
try:
tree = ET.parse(opf_path)
root = tree.getroot()
ns = {
'dc': 'http://purl.org/dc/elements/1.1/',
'opf': 'http://www.idpf.org/2007/opf'
}

title = root.find('.//dc:title', ns)
creators = root.findall('.//dc:creator', ns)
publisher = root.find('.//dcublisher', ns)
date = root.find('.//dc:date', ns)
identifiers = root.findall('.//dc:identifier', ns)

author_lines = [f"%A {c.text.strip()}" for c in creators if c.text]

isbn = None
for ident in identifiers:
text = ident.text.strip() if ident.text else ''
if len(text) in [10, 13] and text.isdigit():
isbn = text
if len(text) == 13:
break

enw_lines = ['%0 Book']
if title is not None and title.text:
enw_lines.append(f"%T {title.text.strip()}")
enw_lines.extend(author_lines)
if date is not None and date.text:
enw_lines.append(f"%D {date.text.strip()}")
if publisher is not None and publisher.text:
enw_lines.append(f"%I {publisher.text.strip()}")
if isbn:
enw_lines.append(f"%@ {isbn}")

# Save file with same base name
enw_path = opf_path.replace('.opf', '.enw')
with open(enw_path, 'w', encoding='utf-8') as f:
f.write('\n'.join(enw_lines) + '\n')
print(f"✔ Converted: {os.path.basename(opf_path)}")
except Exception as e:
print(f"✘ Error processing {opf_path}: {e}")

if __name__ == "__main__":
folder = os.path.dirname(os.path.abspath(__file__))
print(f"📂 Processing .opf files in folder: {folder}\n")

for file in os.listdir(folder):
if file.lower().endswith('.opf'):
opf_to_enw(os.path.join(folder, file))

print("\n✅ Done. .enw files saved in the same folder.")
input("\nPress Enter to close.")
Bill Overal is offline   Reply With Quote
Old 06-20-2025, 04:46 PM   #2
PeterT
Grand Sorcerer
PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.PeterT ought to be getting tired of karma fortunes by now.
 
Posts: 13,471
Karma: 78880114
Join Date: Nov 2007
Location: Toronto
Device: Libra H2O, Libra Colour
Just wrapping the script in a [code]....[/code] block for clarity

Quote:
Originally Posted by Bill Overal View Post
Code:
import xml.etree.ElementTree as ET
import os

def opf_to_enw(opf_path):
    try:
        tree = ET.parse(opf_path)
        root = tree.getroot()
        ns = {
            'dc': 'http://purl.org/dc/elements/1.1/',
            'opf': 'http://www.idpf.org/2007/opf'
        }

        title = root.find('.//dc:title', ns)
        creators = root.findall('.//dc:creator', ns)
        publisher = root.find('.//dc:publisher', ns)
        date = root.find('.//dc:date', ns)
        identifiers = root.findall('.//dc:identifier', ns)

        author_lines = [f"%A {c.text.strip()}" for c in creators if c.text]

        isbn = None
        for ident in identifiers:
            text = ident.text.strip() if ident.text else ''
            if len(text) in [10, 13] and text.isdigit():
                isbn = text
                if len(text) == 13:
                    break

        enw_lines = ['%0 Book']
        if title is not None and title.text:
            enw_lines.append(f"%T {title.text.strip()}")
        enw_lines.extend(author_lines)
        if date is not None and date.text:
            enw_lines.append(f"%D {date.text.strip()}")
        if publisher is not None and publisher.text:
            enw_lines.append(f"%I {publisher.text.strip()}")
        if isbn:
            enw_lines.append(f"%@ {isbn}")

        # Save file with same base name
        enw_path = opf_path.replace('.opf', '.enw')
        with open(enw_path, 'w', encoding='utf-8') as f:
            f.write('\n'.join(enw_lines) + '\n')
        print(f"✔ Converted: {os.path.basename(opf_path)}")
    except Exception as e:
        print(f"✘ Error processing {opf_path}: {e}")

if __name__ == "__main__":
    folder = os.path.dirname(os.path.abspath(__file__))
    print(f"�� Processing .opf files in folder: {folder}\n")

    for file in os.listdir(folder):
        if file.lower().endswith('.opf'):
            opf_to_enw(os.path.join(folder, file))

    print("\n✅ Done. .enw files saved in the same folder.")
    input("\nPress Enter to close.")
PeterT is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatic endnote renumbering after insert bookart Editor 3 08-23-2016 04:14 PM
Endnote display in the viewer roger64 Editor 16 01-23-2016 08:17 AM
Endnote Renumbering Problem GeorgeH Conversion 2 12-31-2013 10:02 AM
Endnote renumbering kerliza Sigil 4 11-02-2013 05:15 PM
Footnote/Endnote debate John123 Workshop 15 01-31-2013 08:55 AM


All times are GMT -4. The time now is 03:06 PM.


MobileRead.com is a privately owned, operated and funded community.