简体   繁体   中英

Hi All, how to copy cells value from 1excel file to an excel file which is Password protected and has some cells which are unprotected & free to use

I'm fairly new to python, I want to copy group of cells value from 1st excel file to 2nd excel file which has sheets & cells as password protected and locked for editing. The 2nd excel file only has those group of cells as unprotected and free for editing. I'm trying this out with openpyxl but the- load_workbook function gives error on the 2nd Excel file as that is password protected for all sheets and most of the cells, I did see that using pandas there is an option of reading only specific columns & from a specific sheet and I'm not sure if that would work or how to go about it here for copy pasting cells value? Is there a way like using Iloc or some other function of pandas by which instead of Reading Columns, I can only let python read those specific group of cells which are allowed for editing and then copy paste the cells value there? Or is there some complete different approach for this which I should try out? Pls see a small sample of code what I'm trying to do-

File 1 copying- 4Rows 4Cols from cells- A1 to E4 File 2 pasting- 4Rows 4Cols from cells G1 to O4

#Copy from file1
file1="D:\\Python Excel copy-paste\\New Excel\\D1.xlsx"
wb1=xl.load_workbook(file1)
ws1=wb1["data"]

#paste to file 2
file2="D:\\Python Excel copy-paste\\New Excel\\D2.xlsx"
wb2=xl.load_workbook(file2)
ws2=wb2["here"]

for i in range(1,5):
    for j in range(2,6):
        
        ws2.cell(row=i,column=j).value=ws1.cell(row=i,column=j).value
                
wb2.save(file2)

You can open a password protected Excel file with Openpyxl using the msoffcrypto tool to decrypt before loading the decrypted workbook into openpyxl in the usual way.

import openpyxl as xl
import io
import msoffcrypto   # pip install msoffcrypto-tool


passwd = 'password'

file2="D:\\Python Excel copy-paste\\New Excel\\D2.xlsx"

file2_decrypted = io.BytesIO()
with open(file2, 'rb') as file:
    d_file = msoffcrypto.OfficeFile(file)
    d_file.load_key(password=passwd)
    d_file.decrypt(file2_decrypted)

    wb2=xl.load_workbook(file2_decrypted)
    ws2=wb2["here"]
    
   ...

You can then manipulate the sheet permissions using Openpyxl if needed. See this answer Python openpyxl - want to protect a sheet but allow filtering, sorting on all cols, and allow editing on column "D" for info on settings these along with the Openpyxl docs . However if you are writing to cells that are unprotected there should be no issue.

Password protecting the workbook when you save is another question. Openpyxl does not support password protecting the workbook to prompt for a password on open. The only other module I've heard of that can do this is Aspose but never used it, but would involve opening and resaving the file. Pandas relies on the underlying engine so if you did use Pandas with Openpyxl, the same limitation applies.

An alternate is to use Xlwings. It can open and save workbooks with password protection and should be able copy the cells you require, but requires Excel to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM