Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added XLS export with xlsxwriter #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions export_report_xls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import FreeCAD
import FreeCADGui

from report_utils.resource_utils import iconPath
from report_utils.selection_utils import findSelectedReportConfig
from report_utils import qtutils

XLSX_FILES = "XLSX Files (*.xlsx)"

class ExportReportXLSCommand:
toolbarName = 'Reporting_Tools'
commandName = 'Export_Report_XLS'

def GetResources(self):
return {'MenuText': "Export Report XLS",
'ToolTip': "Exports the configuration stored inside the Report object to a XLS file",
'Pixmap': iconPath('ExportConfig.svg')
}

def Activated(self):
reportConfig = findSelectedReportConfig()

if reportConfig is None:
qtutils.showInfo(
"No Report selected", "Select exactly one Report object to export its content")

return

selectedFile = qtutils.userSelectedFile(
'Export Location', XLSX_FILES, False)

if selectedFile is None:
return

reportConfig.exportXLS(selectedFile)

def IsActive(self):
"""If there is no active document we can't do anything."""
return not FreeCAD.ActiveDocument is None


if __name__ == "__main__":
command = ExportReportXLSCommand()

if command.IsActive():
command.Activated()
else:
qtutils.showInfo("No open Document", "There is no open document")
else:
import reporting_toolbars
reporting_toolbars.toolbarManager.registerCommand(
ExportReportXLSCommand())
53 changes: 53 additions & 0 deletions report.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,59 @@ def importJson(self, fileObject):
finally:
fileObject.close()

def exportXLS(self, selected_file):
try:
import xlsxwriter

except:
FreeCAD.Console.PrintError("It looks like 'xlsxwriter' could not be found on your system. Please see https://xlsxwriter.readthedocs.io/getting_started.html how to install it.")
return

try:
workbook = xlsxwriter.Workbook(selected_file)
boldFormat = workbook.add_format({'bold': True})
fmts = {}
def _get_format(unit):
if not unit in fmts:
fmts[unit] = workbook.add_format({'num_format': '#.??\\ \\' + unit})
return fmts[unit]

for statement in self.statements:
worksheet = workbook.add_worksheet(statement.header)
lineNumber = 1

if not statement.skipColumnNames:
columnName = None

for columnLabel in statement.getColumnNames():
columnName = nextColumnName(columnName)
cellName = buildCellName(columnName, lineNumber)
worksheet.write_string(cellName, columnLabel, boldFormat)
lineNumber += 1

rows = statement.execute()

for row in rows:
columnName = None

for value in row:
columnName = nextColumnName(columnName)
cellName = buildCellName(columnName, lineNumber)
fmt = None
if isinstance(value, Units.Quantity):
un = value.getUserPreferred()
worksheet.write(cellName, value.Value/un[1], _get_format(un[2]))
elif value is None:
worksheet.write_string(cellName, '')
else:
worksheet.write_string(cellName, str(value))

lineNumber += 1

finally:
workbook.close()


def __getstate__(self):
state = ['VERSION:1']

Expand Down
3 changes: 2 additions & 1 deletion reporting_toolbars.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ def registerCommand(self, command):
# import commands here
import create_report
import export_report_config
import import_report_config
import import_report_config
import export_report_xls