-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_bib_to_bibitem.py
88 lines (76 loc) · 3.06 KB
/
convert_bib_to_bibitem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import tkinter as tk
from tkinter import filedialog
import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import convert_to_unicode
def load_bibtex_file(filename):
""" Load the BibTeX file. """
with open(filename, 'r') as bibfile:
parser = BibTexParser()
parser.customization = convert_to_unicode
bib_database = parser.parse_file(bibfile)
return bib_database
def generate_bibitem(entries):
""" Generate LaTeX \bibitem entries from BibTeX entries. """
bibitems = []
for entry in entries.entries:
author = entry.get('author', '')
year = entry.get('year', '')
title = entry.get('title', '')
journal = entry.get('journal', '')
volume = entry.get('volume', '')
number = entry.get('number', '')
pages = entry.get('pages', '')
publisher = entry.get('publisher', '')
# Handle each type of BibTeX entry
if entry.get('type') == 'article':
bibitem = f"\\bibitem{{{entry.get('ID')}}}\n"
bibitem += f"{author}, ``{title},'' {journal}, vol. {volume}, no. {number}, pp. {pages}, {year}.\n"
elif entry.get('type') == 'book':
bibitem = f"\\bibitem{{{entry.get('ID')}}}\n"
bibitem += f"{author}, \\textit{{{title}}}, {publisher}, {year}.\n"
elif entry.get('type') == 'inproceedings':
bibitem = f"\\bibitem{{{entry.get('ID')}}}\n"
bibitem += f"{author}, ``{title},'' in \\textit{{{journal}}}, {year}.\n"
else:
bibitem = f"\\bibitem{{{entry.get('ID')}}}\n"
bibitem += f"{author}, ``{title},'' {journal}, {year}.\n"
bibitems.append(bibitem)
return bibitems
def save_latex_file(entries, filename):
""" Save the LaTeX \bibitem entries to a file. """
with open(filename, 'w') as latexfile:
latexfile.write("\\begin{thebibliography}{99}\n")
for entry in entries:
latexfile.write(entry)
latexfile.write("\\end{thebibliography}\n")
def main():
# Create a Tkinter root window (it will be hidden)
root = tk.Tk()
root.withdraw()
# Open file dialog to select the input BibTeX file
input_filename = filedialog.askopenfilename(
title="Select the BibTeX file",
filetypes=[("BibTeX files", "*.bib"), ("All files", "*.*")]
)
if not input_filename:
print("No input file selected.")
return
# Load the existing BibTeX file
bib_data = load_bibtex_file(input_filename)
# Generate \bibitem entries
bibitems = generate_bibitem(bib_data)
# Open file dialog to select the output LaTeX file
output_filename = filedialog.asksaveasfilename(
title="Save the LaTeX file",
defaultextension=".bib",
filetypes=[("LaTeX files", "*.bib"), ("All files", "*.*")]
)
if not output_filename:
print("No output file selected.")
return
# Save to a LaTeX file
save_latex_file(bibitems, output_filename)
print(f"File saved as {output_filename}")
if __name__ == "__main__":
main()