BadDataError when converting .DBF to .csv
<p>I am trying to convert a .DBF file to .csv using Python3. I am trying using the dbf library (<a href="https://pypi.python.org/pypi/dbf" rel="nofollow">https://pypi.python.org/pypi/dbf</a>) </p>

<pre><code>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)
</code></pre>

<p>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:</p>

<pre><code>dbf.ver_33.BadDataError: record data is not the correct length (should be 1442, not 1438)
</code></pre>

<p>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?</p>

<p>If the file is opening correctly in Excel, then I suggest you use Excel to do the conversion for you to <code>csv</code> format as follows:</p>

<pre><code>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()
</code></pre>

<p>Do not forget to add full paths to the required files. Note, <code>FileFormat=6</code> tells Excel to save the file in <code>CSV</code> format.</p>

<p>To export the workbook as a PDF, you could use:</p>

<pre><code>wb.ExportAsFixedFormat(0, r"output.pdf")
</code></pre>

<p>If you do not already have <code>win32com</code> installed you should be able to use the following:</p>

<pre><code>pip install pypiwin32
</code></pre>

<p>This solution is suitable for Windows installations only.</p>

python python-3.x dbf