Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I increase the default column width of a csv file so that when I open the file all of the text fits correctly?

I am trying to code a function where I grab data from my database, which already works correctly.

This is my code for the headers prior to adding the actual records:

        with open('csv_template.csv', 'a') as template_file:
        #declares the variable template_writer ready for appending
        template_writer = csv.writer(template_file, delimiter=',')
        #appends the column names of the excel table prior to adding the actual physical data
        template_writer.writerow(['Arrangement_ID','Quantity','Cost'])
    #closes the file after appending
    template_file.close()

This is my code for the records which is contained in a while loop and is the main reason that the two scripts are kept separate.

            with open('csv_template.csv', 'a') as template_file:
            #declares the variable template_writer ready for appending
            template_writer = csv.writer(template_file, delimiter=',')
            #appends the data of the current fetched values of the sql statement within the while loop to the csv file
            template_writer.writerow([transactionWordData[0],transactionWordData[1],transactionWordData[2]])
        #closes the file after appending
        template_file.close()

Now once I have got this data ready for excel, I run the file in excel and I would like it to be in a format where I can print immediately, however, when I do print the column width of the excel cells is too small and leads to it being cut off during printing.

I have tried altering the default column width within excel and hoping that it would keep that format permanently but that doesn't seem to be the case and every time that I re-open the csv file in excel it seems to reset completely back to the default column width.

Here is my code for opening the csv file in excel using python and the comment is the actual code I want to use when I can actually format the spreadsheet ready for printing.

    #finds the os path of the csv file depending where it is in the file directories
    file_path = os.path.abspath("csv_template.csv")
    #opens the csv file in excel ready to print
    os.startfile(file_path)
    #os.startfile(file_path, 'print')

If anyone has any solutions to this or ideas please let me know.

like image 508
Jacob B. Avatar asked Oct 17 '25 21:10

Jacob B.


2 Answers

Unfortunately I don't think this is possible for CSV file formats, since they are just plaintext comma separated values and don't support formatting.

I have tried altering the default column width within excel but every time that I re-open the csv file in excel it seems to reset back to the default column width.

If you save the file to an excel format once you have edited it that should solve this problem.

Alternatively, instead of using the csv library you could use xlsxwriter instead which does allow you to set the width of the columns in your code.

See https://xlsxwriter.readthedocs.io and https://xlsxwriter.readthedocs.io/worksheet.html#worksheet-set-column.

Hope this helps!

like image 65
user10673977 Avatar answered Oct 20 '25 11:10

user10673977


The csv format is nothing else than a text file, where the lines follow a given pattern, that is, a fixed number of fields (your data) delimited by comma. In contrast an .xlsx file is a binary file that contains specifications about the format. Therefore you may want write to an Excel file instead using the rich pandas library.

like image 26
leoburgy Avatar answered Oct 20 '25 09:10

leoburgy