0
votes

I'm new to programming and I'm trying to make a small app where I'll need to copy and paste values to a "stylished" excel woorksheet but when I was testing the first commands, I ran to the first issue:

When I get a cell value from "worksheet1" and paste it into an empty cell from the same worksheet ("worksheet1"), the worksheet stylish kinda messes up (just border cells disapearing), and thats not good, because that table is going to be printed and I need those border cells to stay.

I made a video with the issue: https://www.youtube.com/watch?v=DCZAwYp4zvE

I've tried:

  • Changing the files directory
  • Copying the excel file
  • Making the same but in the other woorksheets
  • I downloaded the original excel from internet so I though it was some incompatibility issue. I even created a new workbook and duplicated from 0 the original workbook (borders, same font, font size, etc), but didnt fix it.

Code:

from openpyxl import load_workbook

file="Test.xlsx"

wb = load_workbook(file)
ws = wb["COMPRA_DIRECTA"]

ws["A80"] = ws["A13"].value


wb.save(file)

Link to the original excel and the script: Link

Im using:

Excel 2016 Openpyxl 2.5.14 Windows

1
Disabel this statement: # ws["A80"] = ws["A13"].value, run again and verify if the worksheet styles messes up. If so, your openpyxl version does not support all of the applied styles. Upgrade to the latest version and try again.stovfl
sorry I was at work, but both anwsers were right, the issue was becaue the openpyxl I was using was too old, thanks!Mario Jose

1 Answers

0
votes

I have used the encoding and it is working fine for me. I have installed Openpyxl 2.6.4 and using LibreOffice Calc on Ubuntu 18.04.

Try this code, it will surely work for you:

from openpyxl import load_workbook
file="Test.xlsx"
wb = load_workbook(file)

# grab the active worksheet
ws = wb["COMPRA_DIRECTA"]

# Data can be assigned directly to cells
ws['A80'] = ws["A13"].value.encode("utf8")

# Save the file
wb.save("sample.xlsx")