Task
Protocols were filled in by hand for every request. It took time and the wording differed from one document to another.
Solution
I wrote an endpoint service in Python. It receives a request, pulls in data through the Google API, runs it through text processing and returns a finished protocol. It is connected to Bitrix, so documents are created within the existing process.
What was done
- Wrote a Python (WSGI) endpoint service that generates protocols
- Pulled in data through the Google API
- Added text processing and assembly of the protocol in one format
- Connected the service to Bitrix to fit the existing process
Result
The protocol for each request is assembled automatically and in one format. Filling in templates by hand is gone from the process.
Developer's note
What was hard
The main difficulty was preserving stable layout across multi-page tables when converting Word documents to PDF via headless LibreOffice on Linux. Test protocols contained a variable number of parameters ranging from three to dozens of rows, causing tables to frequently split across page boundaries. Without low-level XML attributes, Word would split table rows mid-text and omit column headers on subsequent pages. Furthermore, the absence of standard fonts on the host altered line spacing, pushing expert signatures and the blue seal stamp onto an unintended blank page. I resolved this by injecting specific XML properties to prevent row splitting, repeating table headers across every page, and standardizing font metrics inside an isolated container.
Another challenge involved Google Sheets API rate limits and race conditions during simultaneous document generation. Laboratory technicians occasionally updated test methods directly during generation runs, leaving empty cells or invalid data types in tolerance columns. When the Bitrix24 automation dispatched several webhooks in rapid succession, the service exceeded Google API quotas and timed out. I introduced local in-memory caching with automatic cache invalidation for reference catalogs, added strict cell normalization with sensible fallback values, and set up a resilient task queue with exponential backoff retries.
What came easy
Connecting to the Bitrix24 smart process REST API went smoothly. The webhook payload format and methods for reading custom entity fields were straightforward, allowing me to link incoming automation triggers to the Flask backend within a single afternoon. Updating pipeline stages and attaching the generated Word and PDF files back to the CRM item required only minimal boilerplate code.
Designing the templating engine for calculating and inserting test parameters was also simple. Separating the static document layout from the dynamic tabular data made it painless to assemble protocol sections based on method identifiers. When the laboratory introduced new textile density tests, the generator ingested the updated spreadsheet rows seamlessly without any code modifications.
Technologies
PythonGoogle APIBitrix