Color my texts using python

0

Hello, I need that when I write a row with writerow in a file I put the text with a format or a color. And my other request could put color to a certain element of the list from python. I share the code with you:

import csv


def leer():
    m1 = open("archivo1.csv", "r")
    m1_csv = csv.reader(m1)
    var = ""
    for i, x in enumerate(m1_csv):

        lista0 = x[0:1]
        lista1 = x[1:2]
        header = str1 = ''.join(lista0)
        header1 = header[9:13]
        str1 = ''.join(lista1)
        str1 = str1[1:]
        str2 = str1[0:32]

        listas = []
        listas.append("The next header ")
        listas.append(header1)
        listas.append("")
        if header1 == "goin":
            listas.remove("")
            listas.remove("goin")
            listas.append("error")
            listas.append(" the hash is outdate: " + str2)

        if header1 == "omin":
            listas.remove("")
            listas.remove("omin")
            listas.append("error")
            listas.append(" the hash is outdate: " + str2)

        if str2 == "074e3e3e82db7610dbeafd95c22d20a2":
            listas.remove("")
            listas.append(" is using for speaking")
            listas.append(" example bla bla bla the packet")
            listas.append(" Incident: you can get powers")
        else:
            s = "hola"

        listo = ["OUTGOING HEADERS"]
        listos = ["INCOMING HEADERS"]

        m2_c = csv.writer(open("archivo2.csv", "a"), lineterminator='\n')
        if i == 0:
            m2_c.writerow(listo)
        if i == 468:
            m2_c.writerow(listos)
        m2_c.writerow(listas)
    m1.close()

Well, that's where the code is, so I do not have to format the text from python.

    
asked by Sergio Ramos 22.07.2016 в 15:36
source

1 answer

0

The csv files are simple text files with a specific format, do not take into account colors for the text or any special type of font, if you look for that what you need in an xls (x). You could use the library xlsxwritter . An example of its website:

import xlsxwriter


# Create an new Excel file and add a worksheet.
workbook = xlsxwriter.Workbook('demo.xlsx')
worksheet = workbook.add_worksheet()

# Widen the first column to make the text clearer.
worksheet.set_column('A:A', 20)

# Add a bold format to use to highlight cells.
bold = workbook.add_format({'bold': True})

# Write some simple text.
worksheet.write('A1', 'Hello')

# Text with formatting.
worksheet.write('A2', 'World', bold)

# Write some numbers, with row/column notation.
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456)

# Insert an image.
worksheet.insert_image('B5', 'logo.png')

workbook.close()

How do you format text in this case by giving bold in a workbook.

    
answered by 22.07.2016 / 16:43
source