How can I convert the hole EMPTY tables of a GDB into excel file with separate sheets using Python. I have been straggling for at least 4 days! Thank you everyone.
Here is my Python code :
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os
<SPAN class="comment token">#import domainvalues</SPAN>
<SPAN class="comment token"># Set environment settings</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> <SPAN class="string token">"D:\Documents\abid\Travail\_RDC\_BDD"</SPAN>
<SPAN class="comment token"># Set local variables</SPAN>
dataset <SPAN class="operator token">=</SPAN> <SPAN class="string token">"D:\Documents\abid\Travail\_RDC\_BDD\GEOL_GDB.gdb"</SPAN>
output <SPAN class="operator token">=</SPAN> <SPAN class="string token">"D:\Documents\abid\Travail\_RDC\_BDD\EXCEL"</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">export_to_xls</SPAN><SPAN class="punctuation token">(</SPAN>dataset<SPAN class="punctuation token">,</SPAN> output<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Reading Table..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#header, rows = header_and_iterator(dataset)</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddMessage<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Writing Excel File..."</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">_xls</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> xlwt
<SPAN class="keyword token">except</SPAN> ImportError<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>"Import of xlwt module failed<SPAN class="punctuation token">.</SPAN>\nThe XLWT module can\
be downloaded <SPAN class="keyword token">from</SPAN><SPAN class="punctuation token">:</SPAN> http<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">//</SPAN>pypi<SPAN class="punctuation token">.</SPAN>python<SPAN class="punctuation token">.</SPAN>org<SPAN class="operator token">/</SPAN>pypi<SPAN class="operator token">/</SPAN>xlwt"<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN>
<SPAN class="comment token"># Make spreadsheet</SPAN>
workbook <SPAN class="operator token">=</SPAN> xlwt<SPAN class="punctuation token">.</SPAN>Workbook<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
worksheet <SPAN class="operator token">=</SPAN> workbook<SPAN class="punctuation token">.</SPAN>add_sheet<SPAN class="punctuation token">(</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN>dataset<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#Set up header row, freeze panes</SPAN>
header_style <SPAN class="operator token">=</SPAN> xlwt<SPAN class="punctuation token">.</SPAN>easyxf<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"font: bold on; align: horiz center"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> index<SPAN class="punctuation token">,</SPAN> colheader <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>header<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
worksheet<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> index<SPAN class="punctuation token">,</SPAN> colheader<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"."</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"_"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
worksheet<SPAN class="punctuation token">.</SPAN>set_panes_frozen<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
worksheet<SPAN class="punctuation token">.</SPAN>set_horz_split_pos<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
worksheet<SPAN class="punctuation token">.</SPAN>set_remove_splits<SPAN class="punctuation token">(</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Write rows</SPAN>
<SPAN class="keyword token">for</SPAN> rowidx<SPAN class="punctuation token">,</SPAN> row <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>rows<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">for</SPAN> colindex<SPAN class="punctuation token">,</SPAN> col <SPAN class="keyword token">in</SPAN> enumerate<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
worksheet<SPAN class="punctuation token">.</SPAN>write<SPAN class="punctuation token">(</SPAN>rowidx <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> colindex<SPAN class="punctuation token">,</SPAN> col<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># All done</SPAN>
workbook<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>output<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">_xlsx</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> openpyxl
<SPAN class="keyword token">except</SPAN> ImportError<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>"Import of module failed<SPAN class="punctuation token">.</SPAN>\nThe OPENPYXL module can\
be downloaded <SPAN class="keyword token">from</SPAN><SPAN class="punctuation token">:</SPAN> http<SPAN class="punctuation token">:</SPAN><SPAN class="operator token">//</SPAN>pypi<SPAN class="punctuation token">.</SPAN>python<SPAN class="punctuation token">.</SPAN>org<SPAN class="operator token">/</SPAN>pypi<SPAN class="operator token">/</SPAN>openpyxl"<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN>
<SPAN class="comment token"># create spreadsheet</SPAN>
wb <SPAN class="operator token">=</SPAN> openpyxl<SPAN class="punctuation token">.</SPAN>Workbook<SPAN class="punctuation token">(</SPAN>optimized_write<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN>
ws <SPAN class="operator token">=</SPAN> wb<SPAN class="punctuation token">.</SPAN>create_sheet<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># insert the header row</SPAN>
ws<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>header<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># write rows</SPAN>
<SPAN class="keyword token">for</SPAN> row <SPAN class="keyword token">in</SPAN> rows<SPAN class="punctuation token">:</SPAN>
ws<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>row<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># All done</SPAN>
wb<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>output<SPAN class="punctuation token">)</SPAN>a
<SPAN class="keyword token">if</SPAN> output<SPAN class="punctuation token">.</SPAN>endswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'.xls'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
rowcount <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetCount_management<SPAN class="punctuation token">(</SPAN>dataset<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>getOutput<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> rowcount <SPAN class="operator token"><=</SPAN> <SPAN class="number token">65535</SPAN> <SPAN class="operator token">and</SPAN> len<SPAN class="punctuation token">(</SPAN>header<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token"><=</SPAN> <SPAN class="number token">255</SPAN><SPAN class="punctuation token">:</SPAN>
_xls<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN>'Table too large to export to <SPAN class="punctuation token">.</SPAN>xls<SPAN class="punctuation token">.</SPAN>\
Select <SPAN class="punctuation token">.</SPAN>xlsx output <SPAN class="keyword token">for</SPAN> tables larger than <SPAN class="number token">256</SPAN> fields x <SPAN class="number token">65535</SPAN> rows<SPAN class="punctuation token">.</SPAN>'<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
_xlsx<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
dataset_name <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN>
output_file <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>GetParameterAsText<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
export_to_xls<SPAN class="punctuation token">(</SPAN>dataset_name<SPAN class="punctuation token">,</SPAN> output_file<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> err<SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>AddError<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Error: {0}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>err<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>