Thursday, June 10, 2010

[OpenOffice] From database to bibtex entry

For those struggling in the conversion from OpenOffice Bibliography database to bibtex entry. I've generated a small ruby script that makes the translation. This is for open office 3.2.
If any of you knows a more robust tool (probably already embedded in OpenOffice, which I could not find), please let me know.

The process to do this would be:

  1. Find the database file (dbf) in your local directory (usually under the .openoffice.org/3/user/database/biblio/ directory)
  2. Open the file with OpenOffice Calc (or Excel) and export it to CSV (make sure you specify ";" as field delimiter)
  3. Run the following script, putting as first argument in the command file the CSV file that you created in the previous step.
Let me know if you have further questions.

Good luck!



#!/usr/bin/ruby
#-----------------------------------------------------------------------
#
# dbf_to_bibtex.rb
# Script to transform from OpenOffice dbf to bibtex compliant format
#
# Change Log
# 20100610 MGF Created
#
# @ Miquel Garcia-Fernandez
#
#-----------------------------------------------------------------------


# Retrieve the CSV filename to parse
file_name = ARGV[0]
file = File.open( file_name, "r" )

# Field identifiers
identifiers = Array.new

CATEGORY = { "0" => "article",
"1" => "book",
"8" => "techreport",
"16" => "misc", }

file.each_with_index do |line,i_line|

# Retrieve the field identifiers
if i_line==0 then
fields = line.split(";")

fields.each_with_index do |field,i_field|
# Clean from double quotes and remove characters after comma
identifier = field.delete("\"").split(",").flatten[0]
identifiers << identifier.downcase
end

# Go to next line
next
end

# Now parse each field and generate bibtex entry
fields = line.split(";")

# Hash holding the valid entries of the current reference
entries = Hash.new

fields.each_with_index do |field, i_field|
clean_field = field.delete("\"")

next if clean_field.size == 0

entries[ identifiers[i_field] ] = clean_field

end

# Generate bibtex entry for the current line
print "@#{CATEGORY[ entries["type"] ]}{#{entries["identifier"].gsub(/ .*, /,':').gsub(', ',':')},\n"
entries.each_pair do |key,value|
next if key=="type"
next if key=="identifier"
next if key=="isbn"

print " #{key} = \"#{value}\",\n"
end
print "}\n\n"

end



No comments:

Post a Comment