Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6047CEM_Word_Template/bib2xml.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
163 lines (123 sloc)
6.01 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bibtexparser | |
import argparse | |
class Bib2Xml: | |
def __init__(self, _bibFile, _xmlFile): | |
self.bibFile = _bibFile | |
self.xmlFile = _xmlFile | |
def get_bib_entries(self): | |
tmpFile = open(self.bibFile, "r") | |
return bibtexparser.load(tmpFile).entries | |
def parse_bib_entries(self): | |
tmpEntries = self.get_bib_entries() | |
cleanedEntries = [] | |
for e in tmpEntries: | |
tmpEntry = {} | |
for key in e.keys(): | |
if key == "ENTRYTYPE" and e[key] == "inproceedings": | |
tmpEntry["source_type"] = "ConferenceProceedings" | |
elif key == "ENTRYTYPE" and e[key] == "phdthesis": | |
tmpEntry["source_type"] = "Misc" | |
elif key == "ENTRYTYPE" and e[key] == "article": | |
tmpEntry["source_type"] = "JournalArticle" | |
elif key == "ENTRYTYPE" and e[key] == "book": | |
tmpEntry["source_type"] = "Book" | |
elif key == "ENTRYTYPE" and e[key] == "misc": | |
tmpEntry["source_type"] = "InternetSite" | |
if key == "title": | |
tmpEntry["title"] = e[key] | |
if key == "author": | |
authors = [author for author in e[key].split(" and ")] | |
tmpEntry["authors"] = authors | |
if key == "title": | |
tmpEntry["title"] = e[key] | |
if key == "year": | |
tmpEntry["year"] = e[key] | |
if key == "organization": | |
tmpEntry["publisher"] = e[key] | |
if key == "pages": | |
tmpEntry["pages"] = e[key] | |
if key == "journal": | |
tmpEntry["journal_name"] = e[key] | |
elif key == "booktitle": | |
tmpEntry["conference_name"] = e[key] | |
if key == "ID": | |
tmpEntry["tag"] = e[key] | |
if key == "year": | |
tmpEntry["year"] = e[key] | |
if key == "volume": | |
tmpEntry["volume"] = e[key] | |
if key == "number": | |
tmpEntry["issue"] = e[key] | |
if key == "address": | |
tmpEntry["city"] = e[key] | |
if key == "url": | |
tmpEntry["url"] = e[key] | |
if key == "ENTRYTYPE" and e[key] == "misc": | |
tmpEntry["source_type"] = "InternetSite" | |
tmpEntry["internet_site_title"] = e["organization"] | |
dates = e["urldate"].split("-") | |
tmpEntry["year_access"] = dates[0] | |
tmpEntry["month_access"] = dates[1] | |
tmpEntry["day_access"] = dates[2] | |
cleanedEntries.append(tmpEntry) | |
return cleanedEntries | |
def export_xml(self, _entries): | |
tmpFile = open(self.xmlFile, "w") | |
tmpFile.write('<?xml version="1.0"?>') | |
tmpFile.write( | |
'<b:Sources SelectedStyle="" xmlns:b="http://schemas.openxmlformats.org/officeDocument/2006/bibliography" xmlns="http://schemas.openxmlformats.org/officeDocument/2006/bibliography">') | |
for e in _entries: | |
tmpFile.write('<b:Source>') | |
for key in e.keys(): | |
if key == "tag": | |
tmpFile.write(f'<b:Tag>{e[key]}</b:Tag>') | |
if key == "source_type": | |
tmpFile.write(f'<b:SourceType>{e[key]}</b:SourceType>') | |
if key == "journal_name": | |
tmpFile.write(f'<b:JournalName>{e[key]}</b:JournalName>') | |
elif key == "conference_name": | |
tmpFile.write(f'<b:ConferenceName>{e[key]}</b:ConferenceName>') | |
if key == "authors": | |
tmpFile.write(f'<b:Author>') | |
tmpFile.write(f'<b:Author>') | |
tmpFile.write(f'<b:NameList>') | |
for author in e[key]: | |
tmpNames = author.split(", ") | |
tmpFile.write('<b:Person>') | |
tmpFile.write(f'<b:First>{tmpNames[1]}</b:First>') | |
tmpFile.write(f'<b:Last>{tmpNames[0]}</b:Last>') | |
tmpFile.write('</b:Person>') | |
tmpFile.write('</b:NameList>') | |
tmpFile.write('</b:Author>') | |
tmpFile.write('</b:Author>') | |
if key == "title": | |
tmpFile.write(f'<b:Title>{e[key]}</b:Title>') | |
if key == "year": | |
tmpFile.write(f'<b:Year>{e[key]}</b:Year>') | |
if key == "volume": | |
tmpFile.write(f'<b:Volume>{e[key]}</b:Volume>') | |
if key == "issue": | |
tmpFile.write(f'<b:Issue>{e[key]}</b:Issue>') | |
if key == "city": | |
tmpFile.write(f'<b:City>{e[key]}</b:City>') | |
if key == "url": | |
tmpFile.write(f'<b:URL>{e[key]}</b:URL>') | |
if key == "year_access": | |
tmpFile.write(f'<b:YearAccessed>{e[key]}</b:YearAccessed>') | |
if key == "month_access": | |
tmpFile.write(f'<b:MonthAccessed>{e[key]}</b:MonthAccessed>') | |
if key == "day_access": | |
tmpFile.write(f'<b:DayAccessed>{e[key]}</b:DayAccessed>') | |
if key == "internet_site_title": | |
tmpFile.write(f'<b:InternetSiteTitle>{e[key]}</b:InternetSiteTitle>') | |
tmpFile.write('</b:Source>') | |
tmpFile.write('</b:Sources>') | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("-i", "--input", "--bib", help="Provide the BibTex file for parsing.", type=str, required=True) | |
parser.add_argument("-o", "--output", "--xml", help="Provide the name of an XML file for exporting.", type=str, | |
required=True) | |
args = parser.parse_args() | |
bib2xml = Bib2Xml(args.input, args.output) | |
cleanedEntries = bib2xml.parse_bib_entries() | |
bib2xml.export_xml(cleanedEntries) |