@brookmind/ai-toolkit 1.0.5 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/README.md +60 -14
  2. package/agents/code-reviewer.md +6 -1
  3. package/agents/code-simplifier.md +52 -0
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.js +257 -220
  6. package/dist/index.js.map +1 -1
  7. package/mcps/context7/.mcp.json +13 -0
  8. package/mcps/expo-mcp/.mcp.json +13 -0
  9. package/mcps/figma-mcp/.mcp.json +4 -6
  10. package/package.json +4 -4
  11. package/skills/pdf-processing-pro/FORMS.md +610 -0
  12. package/skills/pdf-processing-pro/OCR.md +137 -0
  13. package/skills/pdf-processing-pro/SKILL.md +296 -0
  14. package/skills/pdf-processing-pro/TABLES.md +626 -0
  15. package/skills/pdf-processing-pro/scripts/analyze_form.py +307 -0
  16. package/skills/react-best-practices/AGENTS.md +915 -0
  17. package/skills/react-best-practices/README.md +127 -0
  18. package/skills/react-best-practices/SKILL.md +110 -0
  19. package/skills/react-best-practices/metadata.json +14 -0
  20. package/skills/react-best-practices/rules/_sections.md +41 -0
  21. package/skills/react-best-practices/rules/_template.md +28 -0
  22. package/skills/react-best-practices/rules/advanced-event-handler-refs.md +80 -0
  23. package/skills/react-best-practices/rules/advanced-use-latest.md +76 -0
  24. package/skills/react-best-practices/rules/async-defer-await.md +80 -0
  25. package/skills/react-best-practices/rules/async-dependencies.md +36 -0
  26. package/skills/react-best-practices/rules/async-parallel.md +28 -0
  27. package/skills/react-best-practices/rules/async-suspense-boundaries.md +100 -0
  28. package/skills/react-best-practices/rules/bundle-barrel-imports.md +42 -0
  29. package/skills/react-best-practices/rules/bundle-conditional.md +106 -0
  30. package/skills/react-best-practices/rules/bundle-preload.md +44 -0
  31. package/skills/react-best-practices/rules/client-event-listeners.md +131 -0
  32. package/skills/react-best-practices/rules/client-swr-dedup.md +133 -0
  33. package/skills/react-best-practices/rules/js-batch-dom-css.md +82 -0
  34. package/skills/react-best-practices/rules/js-cache-function-results.md +80 -0
  35. package/skills/react-best-practices/rules/js-cache-property-access.md +28 -0
  36. package/skills/react-best-practices/rules/js-cache-storage.md +70 -0
  37. package/skills/react-best-practices/rules/js-combine-iterations.md +32 -0
  38. package/skills/react-best-practices/rules/js-early-exit.md +50 -0
  39. package/skills/react-best-practices/rules/js-hoist-regexp.md +45 -0
  40. package/skills/react-best-practices/rules/js-index-maps.md +37 -0
  41. package/skills/react-best-practices/rules/js-length-check-first.md +49 -0
  42. package/skills/react-best-practices/rules/js-min-max-loop.md +82 -0
  43. package/skills/react-best-practices/rules/js-set-map-lookups.md +24 -0
  44. package/skills/react-best-practices/rules/js-tosorted-immutable.md +57 -0
  45. package/skills/react-best-practices/rules/rendering-activity.md +90 -0
  46. package/skills/react-best-practices/rules/rendering-animate-svg-wrapper.md +47 -0
  47. package/skills/react-best-practices/rules/rendering-conditional-render.md +40 -0
  48. package/skills/react-best-practices/rules/rendering-content-visibility.md +38 -0
  49. package/skills/react-best-practices/rules/rendering-hoist-jsx.md +65 -0
  50. package/skills/react-best-practices/rules/rendering-svg-precision.md +28 -0
  51. package/skills/react-best-practices/rules/rerender-defer-reads.md +39 -0
  52. package/skills/react-best-practices/rules/rerender-dependencies.md +45 -0
  53. package/skills/react-best-practices/rules/rerender-derived-state.md +29 -0
  54. package/skills/react-best-practices/rules/rerender-functional-setstate.md +74 -0
  55. package/skills/react-best-practices/rules/rerender-lazy-state-init.md +58 -0
  56. package/skills/react-best-practices/rules/rerender-memo.md +85 -0
  57. package/skills/react-best-practices/rules/rerender-transitions.md +40 -0
  58. package/themes/README.md +68 -0
  59. package/themes/claude-vivid.json +72 -0
  60. package/mcps/context7/.claude-plugin +0 -1
  61. package/mcps/context7/README.md +0 -1
  62. package/mcps/context7/server.json +0 -1
  63. package/mcps/expo-mcp/README.md +0 -33
  64. package/mcps/expo-mcp/package.json +0 -30
  65. package/mcps/figma-mcp/README.md +0 -554
  66. package/mcps/figma-mcp/server.json +0 -17
  67. package/mcps/figma-mcp/skills/code-connect-components +0 -1
  68. package/mcps/figma-mcp/skills/create-design-system-rules +0 -1
  69. package/mcps/figma-mcp/skills/implement-design +0 -1
  70. package/mcps/pg-aiguide/.claude-plugin +0 -1
  71. package/mcps/pg-aiguide/CLAUDE.md +0 -21
  72. package/mcps/pg-aiguide/README.md +0 -275
  73. package/mcps/pg-aiguide/skills/design-postgres-tables +0 -1
  74. package/mcps/pg-aiguide/skills/find-hypertable-candidates +0 -1
  75. package/mcps/pg-aiguide/skills/migrate-postgres-tables-to-hypertables +0 -1
  76. package/mcps/pg-aiguide/skills/setup-timescaledb-hypertables +0 -1
  77. package/mcps/pg-aiguide/skills.yaml +0 -4
  78. package/skills/cloudflare-cli/SKILL.md +0 -151
  79. package/skills/docx/LICENSE.txt +0 -30
  80. package/skills/docx/SKILL.md +0 -197
  81. package/skills/docx/docx-js.md +0 -350
  82. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd +0 -1499
  83. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd +0 -146
  84. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd +0 -1085
  85. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd +0 -11
  86. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-main.xsd +0 -3081
  87. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd +0 -23
  88. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd +0 -185
  89. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd +0 -287
  90. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd +0 -1676
  91. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd +0 -28
  92. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd +0 -144
  93. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd +0 -174
  94. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd +0 -25
  95. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd +0 -18
  96. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd +0 -59
  97. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd +0 -56
  98. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesVariantTypes.xsd +0 -195
  99. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-math.xsd +0 -582
  100. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/shared-relationshipReference.xsd +0 -25
  101. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/sml.xsd +0 -4439
  102. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-main.xsd +0 -570
  103. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-officeDrawing.xsd +0 -509
  104. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-presentationDrawing.xsd +0 -12
  105. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-spreadsheetDrawing.xsd +0 -108
  106. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/vml-wordprocessingDrawing.xsd +0 -96
  107. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/wml.xsd +0 -3646
  108. package/skills/docx/ooxml/schemas/ISO-IEC29500-4_2016/xml.xsd +0 -116
  109. package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-contentTypes.xsd +0 -42
  110. package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-coreProperties.xsd +0 -50
  111. package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-digSig.xsd +0 -49
  112. package/skills/docx/ooxml/schemas/ecma/fouth-edition/opc-relationships.xsd +0 -33
  113. package/skills/docx/ooxml/schemas/mce/mc.xsd +0 -75
  114. package/skills/docx/ooxml/schemas/microsoft/wml-2010.xsd +0 -560
  115. package/skills/docx/ooxml/schemas/microsoft/wml-2012.xsd +0 -67
  116. package/skills/docx/ooxml/schemas/microsoft/wml-2018.xsd +0 -14
  117. package/skills/docx/ooxml/schemas/microsoft/wml-cex-2018.xsd +0 -20
  118. package/skills/docx/ooxml/schemas/microsoft/wml-cid-2016.xsd +0 -13
  119. package/skills/docx/ooxml/schemas/microsoft/wml-sdtdatahash-2020.xsd +0 -4
  120. package/skills/docx/ooxml/schemas/microsoft/wml-symex-2015.xsd +0 -8
  121. package/skills/docx/ooxml/scripts/pack.py +0 -159
  122. package/skills/docx/ooxml/scripts/unpack.py +0 -29
  123. package/skills/docx/ooxml/scripts/validate.py +0 -69
  124. package/skills/docx/ooxml/scripts/validation/__init__.py +0 -15
  125. package/skills/docx/ooxml/scripts/validation/base.py +0 -951
  126. package/skills/docx/ooxml/scripts/validation/docx.py +0 -274
  127. package/skills/docx/ooxml/scripts/validation/pptx.py +0 -315
  128. package/skills/docx/ooxml/scripts/validation/redlining.py +0 -279
  129. package/skills/docx/ooxml.md +0 -610
  130. package/skills/docx/scripts/__init__.py +0 -1
  131. package/skills/docx/scripts/document.py +0 -1276
  132. package/skills/docx/scripts/templates/comments.xml +0 -3
  133. package/skills/docx/scripts/templates/commentsExtended.xml +0 -3
  134. package/skills/docx/scripts/templates/commentsExtensible.xml +0 -3
  135. package/skills/docx/scripts/templates/commentsIds.xml +0 -3
  136. package/skills/docx/scripts/templates/people.xml +0 -3
  137. package/skills/docx/scripts/utilities.py +0 -374
  138. package/skills/pdf/LICENSE.txt +0 -30
  139. package/skills/pdf/SKILL.md +0 -294
  140. package/skills/pdf/forms.md +0 -205
  141. package/skills/pdf/reference.md +0 -612
  142. package/skills/pdf/scripts/check_bounding_boxes.py +0 -70
  143. package/skills/pdf/scripts/check_bounding_boxes_test.py +0 -226
  144. package/skills/pdf/scripts/check_fillable_fields.py +0 -12
  145. package/skills/pdf/scripts/convert_pdf_to_images.py +0 -35
  146. package/skills/pdf/scripts/create_validation_image.py +0 -41
  147. package/skills/pdf/scripts/extract_form_field_info.py +0 -152
  148. package/skills/pdf/scripts/fill_fillable_fields.py +0 -114
  149. package/skills/pdf/scripts/fill_pdf_form_with_annotations.py +0 -108
  150. package/skills/xlsx/LICENSE.txt +0 -30
  151. package/skills/xlsx/SKILL.md +0 -289
  152. package/skills/xlsx/recalc.py +0 -178
@@ -1,108 +0,0 @@
1
- import json
2
- import sys
3
-
4
- from pypdf import PdfReader, PdfWriter
5
- from pypdf.annotations import FreeText
6
-
7
-
8
- # Fills a PDF by adding text annotations defined in `fields.json`. See forms.md.
9
-
10
-
11
- def transform_coordinates(bbox, image_width, image_height, pdf_width, pdf_height):
12
- """Transform bounding box from image coordinates to PDF coordinates"""
13
- # Image coordinates: origin at top-left, y increases downward
14
- # PDF coordinates: origin at bottom-left, y increases upward
15
- x_scale = pdf_width / image_width
16
- y_scale = pdf_height / image_height
17
-
18
- left = bbox[0] * x_scale
19
- right = bbox[2] * x_scale
20
-
21
- # Flip Y coordinates for PDF
22
- top = pdf_height - (bbox[1] * y_scale)
23
- bottom = pdf_height - (bbox[3] * y_scale)
24
-
25
- return left, bottom, right, top
26
-
27
-
28
- def fill_pdf_form(input_pdf_path, fields_json_path, output_pdf_path):
29
- """Fill the PDF form with data from fields.json"""
30
-
31
- # `fields.json` format described in forms.md.
32
- with open(fields_json_path, "r") as f:
33
- fields_data = json.load(f)
34
-
35
- # Open the PDF
36
- reader = PdfReader(input_pdf_path)
37
- writer = PdfWriter()
38
-
39
- # Copy all pages to writer
40
- writer.append(reader)
41
-
42
- # Get PDF dimensions for each page
43
- pdf_dimensions = {}
44
- for i, page in enumerate(reader.pages):
45
- mediabox = page.mediabox
46
- pdf_dimensions[i + 1] = [mediabox.width, mediabox.height]
47
-
48
- # Process each form field
49
- annotations = []
50
- for field in fields_data["form_fields"]:
51
- page_num = field["page_number"]
52
-
53
- # Get page dimensions and transform coordinates.
54
- page_info = next(p for p in fields_data["pages"] if p["page_number"] == page_num)
55
- image_width = page_info["image_width"]
56
- image_height = page_info["image_height"]
57
- pdf_width, pdf_height = pdf_dimensions[page_num]
58
-
59
- transformed_entry_box = transform_coordinates(
60
- field["entry_bounding_box"],
61
- image_width, image_height,
62
- pdf_width, pdf_height
63
- )
64
-
65
- # Skip empty fields
66
- if "entry_text" not in field or "text" not in field["entry_text"]:
67
- continue
68
- entry_text = field["entry_text"]
69
- text = entry_text["text"]
70
- if not text:
71
- continue
72
-
73
- font_name = entry_text.get("font", "Arial")
74
- font_size = str(entry_text.get("font_size", 14)) + "pt"
75
- font_color = entry_text.get("font_color", "000000")
76
-
77
- # Font size/color seems to not work reliably across viewers:
78
- # https://github.com/py-pdf/pypdf/issues/2084
79
- annotation = FreeText(
80
- text=text,
81
- rect=transformed_entry_box,
82
- font=font_name,
83
- font_size=font_size,
84
- font_color=font_color,
85
- border_color=None,
86
- background_color=None,
87
- )
88
- annotations.append(annotation)
89
- # page_number is 0-based for pypdf
90
- writer.add_annotation(page_number=page_num - 1, annotation=annotation)
91
-
92
- # Save the filled PDF
93
- with open(output_pdf_path, "wb") as output:
94
- writer.write(output)
95
-
96
- print(f"Successfully filled PDF form and saved to {output_pdf_path}")
97
- print(f"Added {len(annotations)} text annotations")
98
-
99
-
100
- if __name__ == "__main__":
101
- if len(sys.argv) != 4:
102
- print("Usage: fill_pdf_form_with_annotations.py [input pdf] [fields.json] [output pdf]")
103
- sys.exit(1)
104
- input_pdf = sys.argv[1]
105
- fields_json = sys.argv[2]
106
- output_pdf = sys.argv[3]
107
-
108
- fill_pdf_form(input_pdf, fields_json, output_pdf)
@@ -1,30 +0,0 @@
1
- © 2025 Anthropic, PBC. All rights reserved.
2
-
3
- LICENSE: Use of these materials (including all code, prompts, assets, files,
4
- and other components of this Skill) is governed by your agreement with
5
- Anthropic regarding use of Anthropic's services. If no separate agreement
6
- exists, use is governed by Anthropic's Consumer Terms of Service or
7
- Commercial Terms of Service, as applicable:
8
- https://www.anthropic.com/legal/consumer-terms
9
- https://www.anthropic.com/legal/commercial-terms
10
- Your applicable agreement is referred to as the "Agreement." "Services" are
11
- as defined in the Agreement.
12
-
13
- ADDITIONAL RESTRICTIONS: Notwithstanding anything in the Agreement to the
14
- contrary, users may not:
15
-
16
- - Extract these materials from the Services or retain copies of these
17
- materials outside the Services
18
- - Reproduce or copy these materials, except for temporary copies created
19
- automatically during authorized use of the Services
20
- - Create derivative works based on these materials
21
- - Distribute, sublicense, or transfer these materials to any third party
22
- - Make, offer to sell, sell, or import any inventions embodied in these
23
- materials
24
- - Reverse engineer, decompile, or disassemble these materials
25
-
26
- The receipt, viewing, or possession of these materials does not convey or
27
- imply any license or right beyond those expressly granted above.
28
-
29
- Anthropic retains all right, title, and interest in these materials,
30
- including all copyrights, patents, and other intellectual property rights.
@@ -1,289 +0,0 @@
1
- ---
2
- name: xlsx
3
- description: "Comprehensive spreadsheet creation, editing, and analysis with support for formulas, formatting, data analysis, and visualization. When Claude needs to work with spreadsheets (.xlsx, .xlsm, .csv, .tsv, etc) for: (1) Creating new spreadsheets with formulas and formatting, (2) Reading or analyzing data, (3) Modify existing spreadsheets while preserving formulas, (4) Data analysis and visualization in spreadsheets, or (5) Recalculating formulas"
4
- license: Proprietary. LICENSE.txt has complete terms
5
- ---
6
-
7
- # Requirements for Outputs
8
-
9
- ## All Excel files
10
-
11
- ### Zero Formula Errors
12
- - Every Excel model MUST be delivered with ZERO formula errors (#REF!, #DIV/0!, #VALUE!, #N/A, #NAME?)
13
-
14
- ### Preserve Existing Templates (when updating templates)
15
- - Study and EXACTLY match existing format, style, and conventions when modifying files
16
- - Never impose standardized formatting on files with established patterns
17
- - Existing template conventions ALWAYS override these guidelines
18
-
19
- ## Financial models
20
-
21
- ### Color Coding Standards
22
- Unless otherwise stated by the user or existing template
23
-
24
- #### Industry-Standard Color Conventions
25
- - **Blue text (RGB: 0,0,255)**: Hardcoded inputs, and numbers users will change for scenarios
26
- - **Black text (RGB: 0,0,0)**: ALL formulas and calculations
27
- - **Green text (RGB: 0,128,0)**: Links pulling from other worksheets within same workbook
28
- - **Red text (RGB: 255,0,0)**: External links to other files
29
- - **Yellow background (RGB: 255,255,0)**: Key assumptions needing attention or cells that need to be updated
30
-
31
- ### Number Formatting Standards
32
-
33
- #### Required Format Rules
34
- - **Years**: Format as text strings (e.g., "2024" not "2,024")
35
- - **Currency**: Use $#,##0 format; ALWAYS specify units in headers ("Revenue ($mm)")
36
- - **Zeros**: Use number formatting to make all zeros "-", including percentages (e.g., "$#,##0;($#,##0);-")
37
- - **Percentages**: Default to 0.0% format (one decimal)
38
- - **Multiples**: Format as 0.0x for valuation multiples (EV/EBITDA, P/E)
39
- - **Negative numbers**: Use parentheses (123) not minus -123
40
-
41
- ### Formula Construction Rules
42
-
43
- #### Assumptions Placement
44
- - Place ALL assumptions (growth rates, margins, multiples, etc.) in separate assumption cells
45
- - Use cell references instead of hardcoded values in formulas
46
- - Example: Use =B5*(1+$B$6) instead of =B5*1.05
47
-
48
- #### Formula Error Prevention
49
- - Verify all cell references are correct
50
- - Check for off-by-one errors in ranges
51
- - Ensure consistent formulas across all projection periods
52
- - Test with edge cases (zero values, negative numbers)
53
- - Verify no unintended circular references
54
-
55
- #### Documentation Requirements for Hardcodes
56
- - Comment or in cells beside (if end of table). Format: "Source: [System/Document], [Date], [Specific Reference], [URL if applicable]"
57
- - Examples:
58
- - "Source: Company 10-K, FY2024, Page 45, Revenue Note, [SEC EDGAR URL]"
59
- - "Source: Company 10-Q, Q2 2025, Exhibit 99.1, [SEC EDGAR URL]"
60
- - "Source: Bloomberg Terminal, 8/15/2025, AAPL US Equity"
61
- - "Source: FactSet, 8/20/2025, Consensus Estimates Screen"
62
-
63
- # XLSX creation, editing, and analysis
64
-
65
- ## Overview
66
-
67
- A user may ask you to create, edit, or analyze the contents of an .xlsx file. You have different tools and workflows available for different tasks.
68
-
69
- ## Important Requirements
70
-
71
- **LibreOffice Required for Formula Recalculation**: You can assume LibreOffice is installed for recalculating formula values using the `recalc.py` script. The script automatically configures LibreOffice on first run
72
-
73
- ## Reading and analyzing data
74
-
75
- ### Data analysis with pandas
76
- For data analysis, visualization, and basic operations, use **pandas** which provides powerful data manipulation capabilities:
77
-
78
- ```python
79
- import pandas as pd
80
-
81
- # Read Excel
82
- df = pd.read_excel('file.xlsx') # Default: first sheet
83
- all_sheets = pd.read_excel('file.xlsx', sheet_name=None) # All sheets as dict
84
-
85
- # Analyze
86
- df.head() # Preview data
87
- df.info() # Column info
88
- df.describe() # Statistics
89
-
90
- # Write Excel
91
- df.to_excel('output.xlsx', index=False)
92
- ```
93
-
94
- ## Excel File Workflows
95
-
96
- ## CRITICAL: Use Formulas, Not Hardcoded Values
97
-
98
- **Always use Excel formulas instead of calculating values in Python and hardcoding them.** This ensures the spreadsheet remains dynamic and updateable.
99
-
100
- ### ❌ WRONG - Hardcoding Calculated Values
101
- ```python
102
- # Bad: Calculating in Python and hardcoding result
103
- total = df['Sales'].sum()
104
- sheet['B10'] = total # Hardcodes 5000
105
-
106
- # Bad: Computing growth rate in Python
107
- growth = (df.iloc[-1]['Revenue'] - df.iloc[0]['Revenue']) / df.iloc[0]['Revenue']
108
- sheet['C5'] = growth # Hardcodes 0.15
109
-
110
- # Bad: Python calculation for average
111
- avg = sum(values) / len(values)
112
- sheet['D20'] = avg # Hardcodes 42.5
113
- ```
114
-
115
- ### ✅ CORRECT - Using Excel Formulas
116
- ```python
117
- # Good: Let Excel calculate the sum
118
- sheet['B10'] = '=SUM(B2:B9)'
119
-
120
- # Good: Growth rate as Excel formula
121
- sheet['C5'] = '=(C4-C2)/C2'
122
-
123
- # Good: Average using Excel function
124
- sheet['D20'] = '=AVERAGE(D2:D19)'
125
- ```
126
-
127
- This applies to ALL calculations - totals, percentages, ratios, differences, etc. The spreadsheet should be able to recalculate when source data changes.
128
-
129
- ## Common Workflow
130
- 1. **Choose tool**: pandas for data, openpyxl for formulas/formatting
131
- 2. **Create/Load**: Create new workbook or load existing file
132
- 3. **Modify**: Add/edit data, formulas, and formatting
133
- 4. **Save**: Write to file
134
- 5. **Recalculate formulas (MANDATORY IF USING FORMULAS)**: Use the recalc.py script
135
- ```bash
136
- python recalc.py output.xlsx
137
- ```
138
- 6. **Verify and fix any errors**:
139
- - The script returns JSON with error details
140
- - If `status` is `errors_found`, check `error_summary` for specific error types and locations
141
- - Fix the identified errors and recalculate again
142
- - Common errors to fix:
143
- - `#REF!`: Invalid cell references
144
- - `#DIV/0!`: Division by zero
145
- - `#VALUE!`: Wrong data type in formula
146
- - `#NAME?`: Unrecognized formula name
147
-
148
- ### Creating new Excel files
149
-
150
- ```python
151
- # Using openpyxl for formulas and formatting
152
- from openpyxl import Workbook
153
- from openpyxl.styles import Font, PatternFill, Alignment
154
-
155
- wb = Workbook()
156
- sheet = wb.active
157
-
158
- # Add data
159
- sheet['A1'] = 'Hello'
160
- sheet['B1'] = 'World'
161
- sheet.append(['Row', 'of', 'data'])
162
-
163
- # Add formula
164
- sheet['B2'] = '=SUM(A1:A10)'
165
-
166
- # Formatting
167
- sheet['A1'].font = Font(bold=True, color='FF0000')
168
- sheet['A1'].fill = PatternFill('solid', start_color='FFFF00')
169
- sheet['A1'].alignment = Alignment(horizontal='center')
170
-
171
- # Column width
172
- sheet.column_dimensions['A'].width = 20
173
-
174
- wb.save('output.xlsx')
175
- ```
176
-
177
- ### Editing existing Excel files
178
-
179
- ```python
180
- # Using openpyxl to preserve formulas and formatting
181
- from openpyxl import load_workbook
182
-
183
- # Load existing file
184
- wb = load_workbook('existing.xlsx')
185
- sheet = wb.active # or wb['SheetName'] for specific sheet
186
-
187
- # Working with multiple sheets
188
- for sheet_name in wb.sheetnames:
189
- sheet = wb[sheet_name]
190
- print(f"Sheet: {sheet_name}")
191
-
192
- # Modify cells
193
- sheet['A1'] = 'New Value'
194
- sheet.insert_rows(2) # Insert row at position 2
195
- sheet.delete_cols(3) # Delete column 3
196
-
197
- # Add new sheet
198
- new_sheet = wb.create_sheet('NewSheet')
199
- new_sheet['A1'] = 'Data'
200
-
201
- wb.save('modified.xlsx')
202
- ```
203
-
204
- ## Recalculating formulas
205
-
206
- Excel files created or modified by openpyxl contain formulas as strings but not calculated values. Use the provided `recalc.py` script to recalculate formulas:
207
-
208
- ```bash
209
- python recalc.py <excel_file> [timeout_seconds]
210
- ```
211
-
212
- Example:
213
- ```bash
214
- python recalc.py output.xlsx 30
215
- ```
216
-
217
- The script:
218
- - Automatically sets up LibreOffice macro on first run
219
- - Recalculates all formulas in all sheets
220
- - Scans ALL cells for Excel errors (#REF!, #DIV/0!, etc.)
221
- - Returns JSON with detailed error locations and counts
222
- - Works on both Linux and macOS
223
-
224
- ## Formula Verification Checklist
225
-
226
- Quick checks to ensure formulas work correctly:
227
-
228
- ### Essential Verification
229
- - [ ] **Test 2-3 sample references**: Verify they pull correct values before building full model
230
- - [ ] **Column mapping**: Confirm Excel columns match (e.g., column 64 = BL, not BK)
231
- - [ ] **Row offset**: Remember Excel rows are 1-indexed (DataFrame row 5 = Excel row 6)
232
-
233
- ### Common Pitfalls
234
- - [ ] **NaN handling**: Check for null values with `pd.notna()`
235
- - [ ] **Far-right columns**: FY data often in columns 50+
236
- - [ ] **Multiple matches**: Search all occurrences, not just first
237
- - [ ] **Division by zero**: Check denominators before using `/` in formulas (#DIV/0!)
238
- - [ ] **Wrong references**: Verify all cell references point to intended cells (#REF!)
239
- - [ ] **Cross-sheet references**: Use correct format (Sheet1!A1) for linking sheets
240
-
241
- ### Formula Testing Strategy
242
- - [ ] **Start small**: Test formulas on 2-3 cells before applying broadly
243
- - [ ] **Verify dependencies**: Check all cells referenced in formulas exist
244
- - [ ] **Test edge cases**: Include zero, negative, and very large values
245
-
246
- ### Interpreting recalc.py Output
247
- The script returns JSON with error details:
248
- ```json
249
- {
250
- "status": "success", // or "errors_found"
251
- "total_errors": 0, // Total error count
252
- "total_formulas": 42, // Number of formulas in file
253
- "error_summary": { // Only present if errors found
254
- "#REF!": {
255
- "count": 2,
256
- "locations": ["Sheet1!B5", "Sheet1!C10"]
257
- }
258
- }
259
- }
260
- ```
261
-
262
- ## Best Practices
263
-
264
- ### Library Selection
265
- - **pandas**: Best for data analysis, bulk operations, and simple data export
266
- - **openpyxl**: Best for complex formatting, formulas, and Excel-specific features
267
-
268
- ### Working with openpyxl
269
- - Cell indices are 1-based (row=1, column=1 refers to cell A1)
270
- - Use `data_only=True` to read calculated values: `load_workbook('file.xlsx', data_only=True)`
271
- - **Warning**: If opened with `data_only=True` and saved, formulas are replaced with values and permanently lost
272
- - For large files: Use `read_only=True` for reading or `write_only=True` for writing
273
- - Formulas are preserved but not evaluated - use recalc.py to update values
274
-
275
- ### Working with pandas
276
- - Specify data types to avoid inference issues: `pd.read_excel('file.xlsx', dtype={'id': str})`
277
- - For large files, read specific columns: `pd.read_excel('file.xlsx', usecols=['A', 'C', 'E'])`
278
- - Handle dates properly: `pd.read_excel('file.xlsx', parse_dates=['date_column'])`
279
-
280
- ## Code Style Guidelines
281
- **IMPORTANT**: When generating Python code for Excel operations:
282
- - Write minimal, concise Python code without unnecessary comments
283
- - Avoid verbose variable names and redundant operations
284
- - Avoid unnecessary print statements
285
-
286
- **For Excel files themselves**:
287
- - Add comments to cells with complex formulas or important assumptions
288
- - Document data sources for hardcoded values
289
- - Include notes for key calculations and model sections
@@ -1,178 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Excel Formula Recalculation Script
4
- Recalculates all formulas in an Excel file using LibreOffice
5
- """
6
-
7
- import json
8
- import sys
9
- import subprocess
10
- import os
11
- import platform
12
- from pathlib import Path
13
- from openpyxl import load_workbook
14
-
15
-
16
- def setup_libreoffice_macro():
17
- """Setup LibreOffice macro for recalculation if not already configured"""
18
- if platform.system() == 'Darwin':
19
- macro_dir = os.path.expanduser('~/Library/Application Support/LibreOffice/4/user/basic/Standard')
20
- else:
21
- macro_dir = os.path.expanduser('~/.config/libreoffice/4/user/basic/Standard')
22
-
23
- macro_file = os.path.join(macro_dir, 'Module1.xba')
24
-
25
- if os.path.exists(macro_file):
26
- with open(macro_file, 'r') as f:
27
- if 'RecalculateAndSave' in f.read():
28
- return True
29
-
30
- if not os.path.exists(macro_dir):
31
- subprocess.run(['soffice', '--headless', '--terminate_after_init'],
32
- capture_output=True, timeout=10)
33
- os.makedirs(macro_dir, exist_ok=True)
34
-
35
- macro_content = '''<?xml version="1.0" encoding="UTF-8"?>
36
- <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
37
- <script:module xmlns:script="http://openoffice.org/2000/script" script:name="Module1" script:language="StarBasic">
38
- Sub RecalculateAndSave()
39
- ThisComponent.calculateAll()
40
- ThisComponent.store()
41
- ThisComponent.close(True)
42
- End Sub
43
- </script:module>'''
44
-
45
- try:
46
- with open(macro_file, 'w') as f:
47
- f.write(macro_content)
48
- return True
49
- except Exception:
50
- return False
51
-
52
-
53
- def recalc(filename, timeout=30):
54
- """
55
- Recalculate formulas in Excel file and report any errors
56
-
57
- Args:
58
- filename: Path to Excel file
59
- timeout: Maximum time to wait for recalculation (seconds)
60
-
61
- Returns:
62
- dict with error locations and counts
63
- """
64
- if not Path(filename).exists():
65
- return {'error': f'File {filename} does not exist'}
66
-
67
- abs_path = str(Path(filename).absolute())
68
-
69
- if not setup_libreoffice_macro():
70
- return {'error': 'Failed to setup LibreOffice macro'}
71
-
72
- cmd = [
73
- 'soffice', '--headless', '--norestore',
74
- 'vnd.sun.star.script:Standard.Module1.RecalculateAndSave?language=Basic&location=application',
75
- abs_path
76
- ]
77
-
78
- # Handle timeout command differences between Linux and macOS
79
- if platform.system() != 'Windows':
80
- timeout_cmd = 'timeout' if platform.system() == 'Linux' else None
81
- if platform.system() == 'Darwin':
82
- # Check if gtimeout is available on macOS
83
- try:
84
- subprocess.run(['gtimeout', '--version'], capture_output=True, timeout=1, check=False)
85
- timeout_cmd = 'gtimeout'
86
- except (FileNotFoundError, subprocess.TimeoutExpired):
87
- pass
88
-
89
- if timeout_cmd:
90
- cmd = [timeout_cmd, str(timeout)] + cmd
91
-
92
- result = subprocess.run(cmd, capture_output=True, text=True)
93
-
94
- if result.returncode != 0 and result.returncode != 124: # 124 is timeout exit code
95
- error_msg = result.stderr or 'Unknown error during recalculation'
96
- if 'Module1' in error_msg or 'RecalculateAndSave' not in error_msg:
97
- return {'error': 'LibreOffice macro not configured properly'}
98
- else:
99
- return {'error': error_msg}
100
-
101
- # Check for Excel errors in the recalculated file - scan ALL cells
102
- try:
103
- wb = load_workbook(filename, data_only=True)
104
-
105
- excel_errors = ['#VALUE!', '#DIV/0!', '#REF!', '#NAME?', '#NULL!', '#NUM!', '#N/A']
106
- error_details = {err: [] for err in excel_errors}
107
- total_errors = 0
108
-
109
- for sheet_name in wb.sheetnames:
110
- ws = wb[sheet_name]
111
- # Check ALL rows and columns - no limits
112
- for row in ws.iter_rows():
113
- for cell in row:
114
- if cell.value is not None and isinstance(cell.value, str):
115
- for err in excel_errors:
116
- if err in cell.value:
117
- location = f"{sheet_name}!{cell.coordinate}"
118
- error_details[err].append(location)
119
- total_errors += 1
120
- break
121
-
122
- wb.close()
123
-
124
- # Build result summary
125
- result = {
126
- 'status': 'success' if total_errors == 0 else 'errors_found',
127
- 'total_errors': total_errors,
128
- 'error_summary': {}
129
- }
130
-
131
- # Add non-empty error categories
132
- for err_type, locations in error_details.items():
133
- if locations:
134
- result['error_summary'][err_type] = {
135
- 'count': len(locations),
136
- 'locations': locations[:20] # Show up to 20 locations
137
- }
138
-
139
- # Add formula count for context - also check ALL cells
140
- wb_formulas = load_workbook(filename, data_only=False)
141
- formula_count = 0
142
- for sheet_name in wb_formulas.sheetnames:
143
- ws = wb_formulas[sheet_name]
144
- for row in ws.iter_rows():
145
- for cell in row:
146
- if cell.value and isinstance(cell.value, str) and cell.value.startswith('='):
147
- formula_count += 1
148
- wb_formulas.close()
149
-
150
- result['total_formulas'] = formula_count
151
-
152
- return result
153
-
154
- except Exception as e:
155
- return {'error': str(e)}
156
-
157
-
158
- def main():
159
- if len(sys.argv) < 2:
160
- print("Usage: python recalc.py <excel_file> [timeout_seconds]")
161
- print("\nRecalculates all formulas in an Excel file using LibreOffice")
162
- print("\nReturns JSON with error details:")
163
- print(" - status: 'success' or 'errors_found'")
164
- print(" - total_errors: Total number of Excel errors found")
165
- print(" - total_formulas: Number of formulas in the file")
166
- print(" - error_summary: Breakdown by error type with locations")
167
- print(" - #VALUE!, #DIV/0!, #REF!, #NAME?, #NULL!, #NUM!, #N/A")
168
- sys.exit(1)
169
-
170
- filename = sys.argv[1]
171
- timeout = int(sys.argv[2]) if len(sys.argv) > 2 else 30
172
-
173
- result = recalc(filename, timeout)
174
- print(json.dumps(result, indent=2))
175
-
176
-
177
- if __name__ == '__main__':
178
- main()