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












0














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.










share|improve this question






















  • You can create a xlam file add in that looks at the file you open, if it's csv, then autofit the column width.
    – Andreas
    Nov 19 '18 at 18:19










  • Thank-you, I'll experiment with this and post an answer when I have solved it
    – Jacob B.
    Nov 19 '18 at 18:59
















0














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.










share|improve this question






















  • You can create a xlam file add in that looks at the file you open, if it's csv, then autofit the column width.
    – Andreas
    Nov 19 '18 at 18:19










  • Thank-you, I'll experiment with this and post an answer when I have solved it
    – Jacob B.
    Nov 19 '18 at 18:59














0












0








0







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.










share|improve this question













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.







python excel csv format






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 17:48









Jacob B.

113




113












  • You can create a xlam file add in that looks at the file you open, if it's csv, then autofit the column width.
    – Andreas
    Nov 19 '18 at 18:19










  • Thank-you, I'll experiment with this and post an answer when I have solved it
    – Jacob B.
    Nov 19 '18 at 18:59


















  • You can create a xlam file add in that looks at the file you open, if it's csv, then autofit the column width.
    – Andreas
    Nov 19 '18 at 18:19










  • Thank-you, I'll experiment with this and post an answer when I have solved it
    – Jacob B.
    Nov 19 '18 at 18:59
















You can create a xlam file add in that looks at the file you open, if it's csv, then autofit the column width.
– Andreas
Nov 19 '18 at 18:19




You can create a xlam file add in that looks at the file you open, if it's csv, then autofit the column width.
– Andreas
Nov 19 '18 at 18:19












Thank-you, I'll experiment with this and post an answer when I have solved it
– Jacob B.
Nov 19 '18 at 18:59




Thank-you, I'll experiment with this and post an answer when I have solved it
– Jacob B.
Nov 19 '18 at 18:59












2 Answers
2






active

oldest

votes


















3














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!






share|improve this answer





















  • That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
    – Jacob B.
    Nov 19 '18 at 19:02



















0














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.






share|improve this answer





















  • Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
    – Jacob B.
    Nov 19 '18 at 18:18













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380103%2fhow-do-i-increase-the-default-column-width-of-a-csv-file-so-that-when-i-open-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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!






share|improve this answer





















  • That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
    – Jacob B.
    Nov 19 '18 at 19:02
















3














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!






share|improve this answer





















  • That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
    – Jacob B.
    Nov 19 '18 at 19:02














3












3








3






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!






share|improve this answer












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!







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 17:57









user10673977

964




964












  • That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
    – Jacob B.
    Nov 19 '18 at 19:02


















  • That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
    – Jacob B.
    Nov 19 '18 at 19:02
















That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
– Jacob B.
Nov 19 '18 at 19:02




That's great, thank-you for your help! I didnt realise that was the case so I'll go through with this and make the necessary amendments.
– Jacob B.
Nov 19 '18 at 19:02













0














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.






share|improve this answer





















  • Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
    – Jacob B.
    Nov 19 '18 at 18:18


















0














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.






share|improve this answer





















  • Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
    – Jacob B.
    Nov 19 '18 at 18:18
















0












0








0






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 18:02









leoburgy

1107




1107












  • Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
    – Jacob B.
    Nov 19 '18 at 18:18




















  • Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
    – Jacob B.
    Nov 19 '18 at 18:18


















Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
– Jacob B.
Nov 19 '18 at 18:18






Thank-you for your reply, I will go at this problem with your approach and will feedback once I have this working or close to working.
– Jacob B.
Nov 19 '18 at 18:18




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53380103%2fhow-do-i-increase-the-default-column-width-of-a-csv-file-so-that-when-i-open-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

Npm cannot find a required file even through it is in the searched directory