I am new to Python. I need to write some data from my program to a spreadsheet. I\'ve searched online and there seem to be many packages available (xlwt, XlsXcessive, openpy
CSV stands for comma separated values. CSV is like a text file and can be created simply by adding the .CSV extension
for example write this code:
f = open('example.csv','w')
f.write("display,variable x")
f.close()
you can open this file with excel.
Try taking a look at the following libraries too:
xlwings - for getting data into and out of a spreadsheet from Python, as well as manipulating workbooks and charts
ExcelPython - an Excel add-in for writing user-defined functions (UDFs) and macros in Python instead of VBA
You can try hfexcel Human Friendly object-oriented python library based on XlsxWriter:
from hfexcel import HFExcel
hf_workbook = HFExcel.hf_workbook('example.xlsx', set_default_styles=False)
hf_workbook.add_style(
"headline",
{
"bold": 1,
"font_size": 14,
"font": "Arial",
"align": "center"
}
)
sheet1 = hf_workbook.add_sheet("sheet1", name="Example Sheet 1")
column1, _ = sheet1.add_column('headline', name='Column 1', width=2)
column1.add_row(data='Column 1 Row 1')
column1.add_row(data='Column 1 Row 2')
column2, _ = sheet1.add_column(name='Column 2')
column2.add_row(data='Column 2 Row 1')
column2.add_row(data='Column 2 Row 2')
column3, _ = sheet1.add_column(name='Column 3')
column3.add_row(data='Column 3 Row 1')
column3.add_row(data='Column 3 Row 2')
# In order to get a row with coordinates:
# sheet[column_index][row_index] => row
print(sheet1[1][1].data)
assert(sheet1[1][1].data == 'Column 2 Row 2')
hf_workbook.save()
If your need is to modify an existing workbook, the safest way would be to use pyoo. You need to have some libraries installed and it takes a few hoops to jump through but once its set up, this would be bulletproof as you are leveraging the wide and solid API's of LibreOffice / OpenOffice.
Please see my Gist on how to set up a linux system and do some basic coding using pyoo.
Here is an example of the code:
#!/usr/local/bin/python3
import pyoo
# Connect to LibreOffice using a named pipe
# (named in the soffice process startup)
desktop = pyoo.Desktop(pipe='oo_pyuno')
wkbk = desktop.open_spreadsheet("<xls_file_name>")
sheet = wkbk.sheets['Sheet1']
# Write value 'foo' to cell E5 on Sheet1
sheet[4,4].value='foo'
wkbk.save()
wkbk.close()
The easiest way to import the exact numbers is to add a decimal after the numbers in your l1
and l2
. Python interprets this decimal point as instructions from you to include the exact number. If you need to restrict it to some decimal place, you should be able to create a print command that limits the output, something simple like:
print variable_example[:13]
Would restrict it to the tenth decimal place, assuming your data has two integers left of the decimal.
import xlwt
def output(filename, sheet, list1, list2, x, y, z):
book = xlwt.Workbook()
sh = book.add_sheet(sheet)
variables = [x, y, z]
x_desc = 'Display'
y_desc = 'Dominance'
z_desc = 'Test'
desc = [x_desc, y_desc, z_desc]
col1_name = 'Stimulus Time'
col2_name = 'Reaction Time'
#You may need to group the variables together
#for n, (v_desc, v) in enumerate(zip(desc, variables)):
for n, v_desc, v in enumerate(zip(desc, variables)):
sh.write(n, 0, v_desc)
sh.write(n, 1, v)
n+=1
sh.write(n, 0, col1_name)
sh.write(n, 1, col2_name)
for m, e1 in enumerate(list1, n+1):
sh.write(m, 0, e1)
for m, e2 in enumerate(list2, n+1):
sh.write(m, 1, e2)
book.save(filename)
for more explanation: https://github.com/python-excel