0

I am trying to download only the content of the file from my one drive account and store it into a python variable to use in my code without saving it on my hard disk. Help me.

I tried to call #microsoft graph API using HTTP request to fetch file content but got this type of data.

b'PK\x03\x04\x14\x00\x08\x08\x08\x00\xb8\x0emS\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00_rels/.rels\xad\xd2\xc1J\x031\x10\x06\xe0{\x9fb\x99{w\xb6UDd\xb3\xbd\x88\xd0\x9bH}\x80\x90\xcc\xee\x06\x9bLH\xa6Z\xdf\xdeP\n\xbaPV\xc1\x1e3\xf9\xe7\xe7#\xa4\xdd\x1c\xfd\xbez\xa7\x94\x1d\x07\x05\xab\xba\x81\x8a\x82a\xeb\xc2\xa0\xe0u\xf7\xb4\xbc\x87M\xb7h_h\xaf\xa5D\xf2\xe8b\xae\xcaN\xc8\nF\x91\xf8\x80\x98\xcdH^\xe7\x9a#\x85r\xd3s\xf2Z\xca1\r\x18\xb5y\xd3\x03\xe1\xbai\xee0\xfd\xec\x80n\xd2Ym\xad\x82\xb4\xb5+\xa8v\x9f\x91\xfe\xd7\x8d\x9eD[-\x1a\r'Z\xc6T\xb6\x938\xca\xa5\\xa7\x81D\x81e\xf3\\xc6\xf9\x94\xa8K3\xe0e\xd0\xfa\xef \xee{g\xe8\x91\xcd\xc1S\x90K.:\n\x05Kv\x9e\xa4c\x9c\x13\xdd\Sd\x0eY\xd8\xff\xf2D\xa7\xcc\x1c\xe9\xf6\x9a\xa4i\xe2\xdb\xf3\xc1\xc9\xa2=\x8f\xcf\x9aE\x8b\x93\x9f\xd9}\x01PK\x07\x08\xe5r\xf6D\xe8\x00\x00\x00\xd0\x02\x00\x00PK\x03\x04\x14\x00\x08\x08\x08\x00\xb8\x0emS\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x00\x00docProps/core.xml\x85R\xcbN\xc30\x10\xbc\xf3\x15\x91\xef\x89\x9d\xa4-\xc8jS\tPOTBj+\x107co[C\xecX\xb6\xd3\xc7\xdf\xe3\xa4M(P\t\xc9\x87\xdd\x99\xf1\xec\xae\xd7\xe3\xe9A\x95\xd1\x0e\xac\x93\x95\x9e\xa04!(\x02\xcd+!\xf5f\x82V\xcbY|\x87"\xe7\x99\x16\xac\xac4L\xd0\x11\x1c\x9a\x167cn(\xaf,<\xdb\xca\x80\xf5\x12\\x14\x8c\xb4\xa3\xdcL\xd0\xd6{C1v|\x0b\x8a\xb9$(t \xd7\x95U\xcc\x87\xd4n\xb0a\xfc\x93m\x00g\x84\x8c\xb0\x02\xcf\x04\xf3\x0c7\x86\xb1\xe9\x1d\xd1\xd9R\xf0\xde\xd2\xd4\xb6l\r\x04\xc7P\x82\x02\xed\x1dN\x93\x14\x7fk=X\xe5\xae^h\x99\x0b\xa5\x92\xfeh\xe0\xaa\xb4#{\xf5\xc1\xc9^\xb8\xdf\xef\x93}\xdeJC\xff)~\x9d?-\xdaQc\xa9\x9b\xa7\xe2\x80\x8a\xf1\xb9\x11\xca-0\x0f"\n\x06\xf4T\xaec^\xf2\x87\xc7\xe5\x0c\x15\x19\xc9\xd28%qF\x96\xe9\x90\x0en)!oc\xfc\xeb~cx\x8a+[\xd4\x8e)\x968\x06 \x1aa\x8f7\x1a\x01\x8e[i|Xg\xd1\x92?\x80\x90\x97Lo\xea\xf0\xf6\x05\xe8x\xb5h%=\xd4l\xb5d\xce\xcf\xc3\xfe\xd7\x12\xc4\xfd1x\\xc1\xba\xe6\xd4\x19\xfbw\xbap\xf2%

  • this appears to be zipped data (Specifically PKzip, you can tell because it starts with `PK\x03\x04`), you can probably extract it in-memory with zipfile, [here](https://stackoverflow.com/a/34162395/7540911) is an example on how – Nullman Mar 10 '22 at 13:52
  • I tried it but it doesn't work. – Jay Shahid Mar 10 '22 at 13:57
  • if you want any help then saying "it didn't work" will not get you any. what exactly did you try? what exactly was the error? add the full stack trace to your question. what exactly is the file you are trying to get? is it a excel file? – Nullman Mar 10 '22 at 13:59
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 10 '22 at 14:25
  • I have a doc, docx, xls, xlsx, text etc. files in my one drive with some dummy data. I have to fetch some keywords in my file through a script. Until now I am downloading files into my hard drive and after fetching my required data from the file I delete it. What I want here is to get data without saving files into the hard drive. I want to use file content in a variable and after using it I will discard it. – Jay Shahid Mar 10 '22 at 17:29
  • I have tried it with `urllib` library as well. – Jay Shahid Mar 10 '22 at 17:33
  • I got this type of result with `urllib`. `b'PK\x03\x04\x14\x00\x08\x08\x08\x00\xb8\x0emS\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b\x00\x00\x00_rels/.rels\xad\xd2\xc1J\x031\x10\x06\xe0{\x9fb\x99{w\xb6UDd\xb3\xbd\x88\xd0\x9bH}\x80\x` – Jay Shahid Mar 10 '22 at 17:39
  • that looks good then. you are getting the documents as bytes. if you dont want to save them to disk put them in a [bytesio](https://docs.python.org/3/library/io.html#io.BytesIO) buffer and feed that to whatever library you are using to actually read your office documents (office documents are NOT text files), for example you can read xlsx files with [openpyxl](https://pypi.org/project/openpyxl/) – Nullman Mar 13 '22 at 08:23

0 Answers0