Am .08.2016, 14:55 Uhr, schrieb Rania Labib <
rn.l...@gmail.com>:
> Hi,
Hiya,
> I'm going to append 12 lists to columns in an existing excel file and I
> have two questions:
>
> 1- I'm going to open the existing file using these lines of codes
>
> from openpyxl import load_workbook
>
> wb = load_workbook('C:\Users\Rania\Google Drive\ABS
> 2016\Results\Boxplots\All_Views_boxplot.xlsx')
>
> rootdir1 = "C:\Users\R\Google Drive\2016\Results\Facade"
>
> ws = wb['BoxPlot']
>
>
> Do I use this line to save the file with the appended data? will this
> overwrite my existing file or append the new data and replace my
> existing file?
>
> wb.save(('C:\Users\R\Google
> Drive\2016\Results\Boxplots\All_Views_boxplot.xlsx'))
wb.save() will create / overwrite a file.
I suggest you learn to use os.path.join() to manage you filenames
> 2- I'm appending 12 lists to columns using this line
>
> for row in zip(m_list[1],m_list[2],m_list[3],
> m_list[4],m_list[5],m_list[6],m_list[7],
> m_list[8],m_list[9],m_list[10],m_list[11],m_list[12]):
>
> ws.append(row)
Looks ugly!
> This appends the lists starting from the cell at column 0, row 0.
> However,
> I want to append the lists starting at cell (row 50 and column 2).
Well, you can always just use ws.cell(row=x, column=y, value=v) But this
requires you to have a counter. Your loop could probably be written using
something like this:
for idx, v in enumerate(m_list, 1):
ws.append(m_list[idx:idx+1])
or
for idx, v in enumerate(m_list, 50):
ws.cell(row=idx, column=2, value=m_list[idx-49]) # adjust as required
ws.append() itself depends upon the ws.max_row which is a property that
will be calculated if unknown, but can otherwise be set using a private
attribute.
I'm not sure I understand your follow up question. Do you want to save a
new *workbook* for each text file? Or add a new sheet for each text file?
Saving workbooks always creates a new file and should probably be avoided
in the latter case.
As I said above, use os.path to manage filenames and hopefully keep
problems related to them to a minimum.
folder = r"C:\Users\R\Google Drive\2016\Results\Boxplots") # Windows path
so use a "raw" string
fname = "{0}_boxplot.xlsx".format(text_filename)
wb.save(os.path.joing(folder, fname))
Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel:
+49-211-600-3657
Mobile:
+49-178-782-6226