I have been trying to copy a cell from one workbook to another with formatting only to fail. I have tried numerous suggestion but nothing works.
I have tried:
def copyCellStyle(new_cell,cell):
new_cell.font = copy(cell.font)
new_cell.border = copy(cell.border)
new_cell.fill = copy(cell.fill)
new_cell.number_format = copy(cell.number_format)
new_cell.protection = copy(cell.protection)
new_cell.alignment = copy(cell.alignment)
then use it like:
new_cell.value=old_cell.value
copyCellStyle(new_cell,old_cell)
It runs without errors but the job is not done.
I get errors with the other methods in the styles class. When
new_cell._style = copy(old_cell._style)
this is used I get this error while I am saving the copied file.
Traceback (most recent call last):
File "C:\Python Programs\test\test2.py", line 40, in <module>
wb2.save("Test3.xlsx")
File "C:\Anaconda3\lib\site-packages\openpyxl\workbook\workbook.py", line 392, in save
save_workbook(self, filename)
File "C:\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 293, in save_workbook
writer.save()
File "C:\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 275, in save
self.write_data()
File "C:\Anaconda3\lib\site-packages\openpyxl\writer\excel.py", line 84, in write_data
stylesheet = write_stylesheet(self.workbook)
File "C:\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 240, in write_stylesheet
xf.alignment = wb._alignments[style.alignmentId]
IndexError: list index out of range
Also when I check if cell has style like:
Traceback (most recent call last)
File "C:\Python Programs\test\test2.py", line 37, in <module>
if cell.has_style:
AttributeError: module 'openpyxl.cell' has no attribute 'has_style'
I am using python 3.x.
Can anyone tell me a working proper method to achieve this.
Thanks in advance!