Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Hand bank data from Python to Excel as XLSX, not CSV, to keep references intact

If a Python pipeline hands bank transactions to someone who works in Excel, write an XLSX file rather than a CSV. A CSV carries no column types, so every person who double-clicks it lets Excel guess again: references lose leading zeros, 16-digit identifiers end in 0, dates can flip. An XLSX stores each cell's type, so you decide once, in code. Quoting fields in the CSV does not change this.…

When Python transfers bank transaction data to someone who uses Excel, save the file as an XLSX instead of a CSV. A CSV file does not preserve column types, causing Excel to misinterpret data, including leading zeros, long identifiers, and date formats. An XLSX file, however, retains each cell's type, ensuring proper handling of data cells.

To create the XLSX file, use openpyxl and load_workbook modules in Python. Define fictional transaction rows, including date, reference, description, and amount. Append the header row to the active worksheet, then iterate through the transaction rows, appending each row to the worksheet. Set the date format to yyyy-mm-dd, use "@" for the text format of the reference, and store the description as a string using data_type = "s".

Save the workbook as "handover.xlsx". To verify the file, load it back into Python and iterate through the rows, printing the data type of each cell along with the actual values. This confirms that dates, strings, and numbers are correctly preserved in the XLSX file.

When creating the XLSX file, ensure amounts are stored as Python Decimal values to avoid rounding issues. Dates should be written as real date objects, while the display format should be yyyy-mm-dd for unambiguous reading. Recheck totals by recomputing debits, credits, and closing balance after saving the file. Only use a CSV when the receiving system specifically requests it; in that case, add a short field note detailing the reference type, date format, decimal mark, and sign convention. Always test the CSV through the recipient's import route to ensure proper handling.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

More from Friday 27 November →