@Danielm103
1. about aligning the image to the center of the cell:
I changed the X offset value (A process of trial and error and depends on the length of the header text):
{"x_offset": 35, "y_offset": 0, "x_scale": 0.8, "y_scale": 0.8}
2. the first column was invisible because "val" for those cells was empty after the header's row and those tow lines return empty string after the first row which set the column width to 0:
w, h =table.calcTextExtents(val,table.textStyle(cell.row, cell.column))
ws.set_column(cell.column,cell.column, w, cell_format)
so I did this change with "autofit":
else:
#val = table.textString(cell.row, cell.column)
#try to get a text width
#w, h =table.calcTextExtents(val,table.textStyle(cell.row, cell.column))
ws.set_column(cell.column,cell.column, 1, cell_format)#minimum width,autofit wil set at the end
ws.write(cell.row, cell.column, table.textString(cell.row, cell.column))
ws.autofit()
wb.close()
here is the final code I'm using:
#https://www.cadtutor.net/forum/topic/97450-export-table-with-blocks-to-excel-with-python/
from pyrx import Rx, Ge, Gi, Gs, Db, Ap, Ed, Ax, Brx
import traceback
import pathlib
import xlsxwriter
import ctypes
import wx
@Ap.Command()
def Py_tablewithimagetoexcel_xlsxwriter():
try:
db = Db.curDb()
ps, id, _ = Ed.Editor.entSel("\nSelect a table: ", Db.Table.desc())
if ps != Ed.PromptStatus.eOk:
raise RuntimeError("Selection Error! {}: ".format(ps))
xlpath = pathlib.Path(db.getFilename()).parent
xlname = pathlib.Path(db.getFilename()).stem
fpt = "{}/{}.xlsx".format(xlpath, xlname)
wb = xlsxwriter.Workbook(fpt) # create workbook
ws = wb.add_worksheet() # create worksheet in workbook
ws.set_default_row(40) # set default row height to 39.6
cell_format = wb.add_format() # add format
cell_format.set_align("center")
cell_format.set_align("vcenter")
table = Db.Table(id)
opts = Db.TableIteratorOption.kTableIteratorSkipMerged
for cell in table.cells(opts):
if table.cellType(cell.row, cell.column) == Db.CellType.kBlockCell:
ws.set_column(cell.column,cell.column, 15.44, cell_format)
blk = table.blockTableRecordId(cell.row, cell.column)
bname = getEffectiveNameFromBtrId(blk)
img: wx.Image = Gs.Core.getBlockImage(blk, 64, 64, 1.0, [0, 0, 0])
img.SetMaskColour(0, 0, 0)
img.SetMask(True)
imgpath = "{}/{}.png".format(xlpath, bname)
img.SaveFile(imgpath, wx.BITMAP_TYPE_PNG)
ws.insert_image(
cell.row,
cell.column,
imgpath,
{"x_offset": 35, "y_offset": 0, "x_scale": 0.8, "y_scale": 0.8},
)
else:
#val = table.textString(cell.row, cell.column)
#try to get a text width
#w, h =table.calcTextExtents(val,table.textStyle(cell.row, cell.column))
ws.set_column(cell.column,cell.column, 1, cell_format)#keep format,set minimum width,autofit will set at the end
ws.write(cell.row, cell.column, table.textString(cell.row, cell.column))
ws.autofit()
wb.close()
ctypes.windll.user32.MessageBoxW(0, fpt , "Files Saved", 1)
except Exception as err:
traceback.print_exception(err)
def getEffectiveNameFromBtrId(btrid: Db.ObjectId):
rec = Db.BlockTableRecord(btrid)
if rec.isAnonymous():
ids = rec.getBlockReferenceIds()
if len(ids) > 0 and Brx.DbProperties.isValid(ids[0], "EffectiveName~Native"):
val = Brx.DbProperties.getValue(ids[0], "EffectiveName~Native")
return val.getString()
return rec.getName()
Thanks for all your help!!
aridzv.