'xlsxwriter: How to use another module to write into worksheet created in main code
I was curious if there was a way to use another module to write into an excel worksheet made in the main code. So for example, I have the following code that creates the excel file:
# Package
import xlsxwriter
# Module for this code
import excel_edit
# Main Code
list = [1, 2, 3, 4, 5]
workbook = xlsxwriter.Workbook('test.xlsx')
sheet_1 = workbook.add_worksheet
excel_edit.write_sheet(sheet_1, list) # Function taken from excel_edit module
workbook.close()
And here is the code for the module:
import xlsxwriter
def write_sheet(worksheet, list)
# Create label
worksheet.write(0, 0, 'Time (sec)
# Code here to loop and add list elements row by row
When I run this, the excel file isn't being created at all and from what I discovered the import xlsxwriter in my excel_edit module isn't actually being used. Is there a way to still do this method, or should I just shove all the code into main lol.
Solution 1:[1]
This calls your function from other module as expected:
import xlsxwriter
import excel_edit
alist = [1, 2, 3, 4, 5]
workbook = xlsxwriter.Workbook('test.xlsx')
sheet_1 = workbook.add_worksheet()
excel_edit.write_sheet(sheet_1, alist)
workbook.close()
Note the () in workbook.add_worksheet
You need to call this function, else you pass the function itself to excel_edit.write_sheet
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | arise21 |
