-
Notifications
You must be signed in to change notification settings - Fork 92
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
Feature xlsx #57
Merged
SchrodingersGat
merged 8 commits into
SchrodingersGat:master
from
Ximi1970:feature-xlsx
May 30, 2019
Merged
Feature xlsx #57
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f74fba
Added XLSX output
Ximi1970 dc027c6
Left jusitfy bottom headers
Ximi1970 34f74cd
Fix column width
Ximi1970 55775ad
Make XLSX optional. XlsxWriter automaticly detected.
Ximi1970 4c0c63e
Cleanup
Ximi1970 c9919ca
Fix import error
Ximi1970 e03daed
Remove print
Ximi1970 a326349
Cleanup
Ximi1970 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# _*_ coding:latin-1 _*_ | ||
|
||
try: | ||
import xlsxwriter | ||
except: | ||
def WriteXLSX(filename, groups, net, headings, prefs): | ||
return False | ||
else: | ||
import bomlib.columns as columns | ||
from bomlib.component import * | ||
import os, shutil | ||
from bomlib.preferences import BomPref | ||
|
||
""" | ||
Write BoM out to a XLSX file | ||
filename = path to output file (must be a .xlsx file) | ||
groups = [list of ComponentGroup groups] | ||
net = netlist object | ||
headings = [list of headings to display in the BoM file] | ||
prefs = BomPref object | ||
""" | ||
|
||
def WriteXLSX(filename, groups, net, headings, prefs): | ||
|
||
filename = os.path.abspath(filename) | ||
|
||
if not filename.endswith(".xlsx"): | ||
return False | ||
|
||
nGroups = len(groups) | ||
nTotal = sum([g.getCount() for g in groups]) | ||
nFitted = sum([g.getCount() for g in groups if g.isFitted()]) | ||
nBuild = nFitted * prefs.boards | ||
|
||
workbook = xlsxwriter.Workbook(filename) | ||
worksheet = workbook.add_worksheet() | ||
|
||
if prefs.numberRows: | ||
row_headings = ["Component"] + headings | ||
else: | ||
row_headings = headings | ||
|
||
cellformats = {} | ||
column_widths = {} | ||
for i in range(len(row_headings)): | ||
cellformats[i] = workbook.add_format({'align':'center_across'}) | ||
column_widths[i] = len(row_headings[i]) + 10 | ||
|
||
if not prefs.hideHeaders: | ||
worksheet.write_string( 0, i, row_headings[i], cellformats[i]) | ||
|
||
count = 0 | ||
rowCount = 1 | ||
|
||
for i, group in enumerate(groups): | ||
if prefs.ignoreDNF and not group.isFitted(): continue | ||
|
||
row = group.getRow(headings) | ||
|
||
if prefs.numberRows: | ||
row = [str(rowCount)] + row | ||
|
||
for columnCount in range(len(row)): | ||
|
||
cell = row[columnCount].decode('utf-8') | ||
|
||
worksheet.write_string(rowCount,columnCount,cell,cellformats[columnCount]) | ||
|
||
if len(cell) > column_widths[columnCount] - 5: | ||
column_widths[columnCount] = len(cell) + 5 | ||
|
||
try: | ||
count += group.getCount() | ||
except: | ||
pass | ||
|
||
rowCount += 1 | ||
|
||
if not prefs.hideHeaders: | ||
#blank rows | ||
for i in range(5): | ||
rowCount += 1 | ||
|
||
cellformat_left = workbook.add_format({'align':'left'}) | ||
|
||
worksheet.write_string( rowCount, 0, "Component Groups:", cellformats[0]) | ||
worksheet.write_number( rowCount, 1, nGroups, cellformat_left) | ||
rowCount += 1 | ||
|
||
worksheet.write_string( rowCount, 0, "Component Count:", cellformats[0]) | ||
worksheet.write_number( rowCount, 1, nTotal, cellformat_left) | ||
rowCount += 1 | ||
|
||
worksheet.write_string( rowCount, 0, "Fitted Components:", cellformats[0]) | ||
worksheet.write_number( rowCount, 1, nFitted, cellformat_left) | ||
rowCount += 1 | ||
|
||
worksheet.write_string( rowCount, 0, "Number of PCBs:", cellformats[0]) | ||
worksheet.write_number( rowCount, 1, prefs.boards, cellformat_left) | ||
rowCount += 1 | ||
|
||
worksheet.write_string( rowCount, 0, "Total components:", cellformats[0]) | ||
worksheet.write_number( rowCount, 1, nBuild, cellformat_left) | ||
rowCount += 1 | ||
|
||
worksheet.write_string( rowCount, 0, "Schematic Version:", cellformats[0]) | ||
worksheet.write_string( rowCount, 1, net.getVersion(), cellformat_left) | ||
rowCount += 1 | ||
|
||
if len(net.getVersion()) > column_widths[1]: | ||
column_widths[1] = len(net.getVersion()) | ||
|
||
|
||
worksheet.write_string( rowCount, 0, "Schematic Date:", cellformats[0]) | ||
worksheet.write_string( rowCount, 1, net.getSheetDate(), cellformat_left) | ||
rowCount += 1 | ||
|
||
if len(net.getSheetDate()) > column_widths[1]: | ||
column_widths[1] = len(net.getSheetDate()) | ||
|
||
|
||
worksheet.write_string( rowCount, 0, "BoM Date:", cellformats[0]) | ||
worksheet.write_string( rowCount, 1, net.getDate(), cellformat_left) | ||
rowCount += 1 | ||
|
||
if len(net.getDate()) > column_widths[1]: | ||
column_widths[1] = len(net.getDate()) | ||
|
||
worksheet.write_string( rowCount, 0, "Schematic Source:", cellformats[0]) | ||
worksheet.write_string( rowCount, 1, net.getSource(), cellformat_left) | ||
rowCount += 1 | ||
|
||
if len(net.getSource()) > column_widths[1]: | ||
column_widths[1] = len(net.getSource()) | ||
|
||
worksheet.write_string( rowCount, 0, "KiCad Version:", cellformats[0]) | ||
worksheet.write_string( rowCount, 1, net.getTool(), cellformat_left) | ||
rowCount += 1 | ||
|
||
if len(net.getTool()) > column_widths[1]: | ||
column_widths[1] = len(net.getTool()) | ||
|
||
for i in range(len(column_widths)): | ||
worksheet.set_column( i, i, column_widths[i]) | ||
|
||
workbook.close() | ||
|
||
return True |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have a default extensions array, and then optionally append '.xlsx' if xlsxwriter_available is True