I am trying to convert a .DBF file to .csv using Python3. I am trying using the dbf library (https://pypi.python.org/pypi/dbf)
import dbf
def dbf_to_csv(dbf_file_name, csv_file_name):
dbf_file = dbf.Table(dbf_file_name, ignore_memos=True)
dbf_file.open()
dbf.export(dbf_file, filename = csv_file_name, format='csv', header=True)
The DBF file I am using can be opened in Excel and appears to be fine. However, when I run the above method I get an error on the dbf.export line above:
dbf.ver_33.BadDataError: record data is not the correct length (should be 1442, not 1438)
The dbf file opens fine in Excel, however, I need to automate this conversion. What should I be doing differently to get this method to create a pdf from a .DBF file?
If the file is opening correctly in Excel, then I suggest you use Excel to do the conversion for you to csv
format as follows:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r"input.dbf")
excel.DisplayAlerts = False
wb.DoNotPromptForConvert = True
wb.CheckCompatibility = False
wb.SaveAs(r"output.csv", FileFormat=6, ConflictResolution=2)
excel.Application.Quit()
Do not forget to add full paths to the required files. Note, FileFormat=6
tells Excel to save the file in CSV
format.
To export the workbook as a PDF, you could use:
wb.ExportAsFixedFormat(0, r"output.pdf")
If you do not already have win32com
installed you should be able to use the following:
pip install pypiwin32
This solution is suitable for Windows installations only.