Skip to content

Instantly share code, notes, and snippets.

@ilyasProgrammer
Created April 29, 2021 11:28
Show Gist options
  • Save ilyasProgrammer/824e999844a2e9637cc3125b09013f9b to your computer and use it in GitHub Desktop.
Save ilyasProgrammer/824e999844a2e9637cc3125b09013f9b to your computer and use it in GitHub Desktop.
Rotate CSV from attachment
atts = self.env['ir.attachment'].search([('res_model', '=', 'syscoon.financeinterface'),
('type', '=', 'binary'),
('res_id', '=', export_id)])
for at in atts:
# order lines by date asc. just revert CSV file data rows.
data = base64.decodebytes(at.datas).decode("latin")
stream = StringIO(data)
dialect = csv.Sniffer().sniff(data[:5000])
data_list = list(csv.reader(stream, dialect=dialect))
file_path = "/var/tmp/" + at.name
with open(file_path, 'w') as w:
write = csv.writer(w, delimiter=";", dialect='excel')
write.writerow(data_list[0])
write.writerow(data_list[1])
write.writerows(reversed(data_list[2:]))
with open(file_path, 'rb') as r:
at.datas = base64.b64encode(r.read())
os.remove(file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment